ES5-ES6-ES7_数组的扩展
Array.prototype.indexOf(value)
得到值在数组中的第一个下标,如果元素不存在返回-1,可以用来判断是否包含指定的元素
var arr = [6,5,4,3,1,7,6]; console.log(arr.indexOf(5))
Array.prototype.includes(value)
判断数组中是否包含指定value
var arr = [6,5,4,3,1,7,6]; console.log(arr.includes(5)) // true console.log(arr.includes(8)) // false
Array.prototype.lastIndexOf(value)
得到值在数组中的最后一个下标
var arr = [6,5,4,3,1,7,6]; console.log(arr.lastIndexOf(6))
Array.prototype.forEach(function(item, index){})
遍历数组
var arr = [6,5,4,3,1,7,6];
arr.forEach(function (item, index) {
console.log(item + '-' + index) // index是角标,item是元素值
})
Array.prototype.map(function(item, index){})
遍历数组返回一个新的数组,返回加工之后的值
var arr = [6,5,4,3,1,7,6];
var newArr = arr.map(function (item, index) {
return item + 10
})
console.log(newArr)
Array.prototype.filter(function(item, index){})
遍历过滤出一个新的子数组, 返回条件为true的值
var arr = [6,5,4,3,1,7,6];
var newArr = arr.filter(function (item, index) {
return item > 5 //将arr数组中所有大于5的元素添加到一个新的数组中
})
console.log(newArr)
Array.from(v)
将伪数组对象或可遍历对象转换为真数组
<html lang="en">
<head>
<meta charset="UTF-8">
<title>03_数组扩展</title>
</head>
<body>
<button>测试1</button>
<br>
<button>测试2</button>
<br>
<button>测试3</button>
<br>
<script type="text/javascript">
let btns = document.getElementsByTagName('button') //现在btns也是一个数组(伪数组,没有数组一般的方法)
// 因为btns是个伪数组,没有数组的一般方法,所以会报错
// btns.forEach(function (item, index) {
// console.log(item)
// })
// 将伪数组对象转换为真数组
Array.from(btns).forEach(function (item, index) {
console.log(item)
})
</script>
</body>
</html>
Array.of(v1, v2, v3)
将一系列值转换成数组
console.log(Array.of('3', 3, '4')) //["3", 3, "4"]
find(function(value, index, arr){return true})和findIndex(function(value, index, arr){return true})
find:找出第一个满足条件返回true的元素
findIndex:找出第一个满足条件返回true的元素下标
let arr = [4,5,1,6,4,7,9,13]
let result = arr.find(function (item, index) {
return item > 3
})
let resultIndex = arr.findIndex(function (item, index) {
return item > 3
})
console.log(result) //4,也就是数组中的第一个元素的值,满足了条件大于3
console.log(resultIndex) //0,也就是数组中的第一个元素的下标,满足了条件大于3
ES5-ES6-ES7_数组的扩展的更多相关文章
- ES6对数组的扩展(简要总结)
文章目录 数组的扩展(ES6) 1. 扩展运算符 2. Array.from 3. Array.of() 4. copyWithin() 5. find() 和 findIndex() 6. fill ...
- ES6 之 数组的扩展
ES5 检测数组 let arr = [1,2,3,4] Array.isArray(arr) arr instanceof Array 转换方法 arr.toLocaleString() arr.t ...
- ES6对数组的扩展
ECMAScript6对数组进行了扩展,为数组Array构造函数添加了from().of()等静态方法,也为数组实例添加了find().findIndex()等方法.下面一起来看一下这些方法的用法. ...
- 【ES6】数组的扩展——扩展运算符
1.扩展运算符[三个点(...)将一个数组转为用逗号分隔的参数序列] 作用:用于函数调用 function add(x, y) { return x + y; } const numbers = [2 ...
- 【ES6】数组的扩展
1.Array.from(): 将伪数组对象和遍历的对象转为真数组 如果一个对象的键都是正整数或者0,并且有 Length属性,那么这个对象很想数组,称它为伪数组. 伪数组: let obj = { ...
- 数组的复制及ES6数组的扩展
一.数组的复制 // alert([1,2,3]==[1,2,3]); let cc = [0,1,2]; let dd = cc; alert(dd==cc);//此时改变dd会影响cc ES5 只 ...
- ES6的新特性(8)——数组的扩展
数组的扩展 扩展运算符 含义 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) / ...
- ES6数组的扩展运算符
一.基本使用 ES6中函数可以使用 rest参数 接收函数的多余参数,组成一个数组,放在形参的最后面. let fn = (a, ...value) => { console.log(a); c ...
- ES6学习(三):数组的扩展
chapter08 数组的扩展 8.1 扩展运算符 8.1.1 扩展运算符的含义 ... 如同rest运算符的逆运算,将一个数组转换为用逗号分隔的参数序列. console.log(...[1, 2, ...
- ES6学习笔记(六)数组的扩展
1.扩展运算符 1.1含义 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) // ...
随机推荐
- .net 公共基础类
using WL.Infrastructure.Http; using System; using System.Collections.Generic; using System.IO; using ...
- 如何去掉C#字符串中的所有空格
字符串行数Trim()可以去掉字符串前后的空格,如: C# Code string myString = " this is a test "; Console.WriteLi ...
- mybatis笔记02
目录 0. 文章目录 1. Mybatis映射文件 1.1 输入映射 1.2 输出映射 1.3 resultMap 2. 动态SQL 2.1 if和where 2.2 foreach循环 2.3 sq ...
- 持续集成 自动化构建、测试、部署您的Coding代码
持续集成(Continuous Integration)指的是,频繁地(一天多次)将代码集成到主干. 持续集成的目的,就是让产品可以快速迭代,同时还能保持高质量. 它的核心措施是,代码集成到主干之前, ...
- URL 与 URI 介绍
URL: 统一资源定位符 ( Uniform Resource Locator ) URI: 统一资源标识符 ( Uniform Resource Identifier ) URL 地址:https: ...
- 如何去除vue项目中的 # --- History模式
来自:https://www.cnblogs.com/zhuzhenwei918/p/6892066.html 侵删 使用vue-cli搭建的环境,在配置好路由之后,可以看到下面的情况: 但是不难发现 ...
- 洛谷P4590 [TJOI2018]游园会(状压dp LCS)
题意 题目链接 Sol 这个题可能是TJOI2018唯一的非模板题了吧.. 考虑LCS的转移方程, \[f[i][j] = max(f[i - 1][j], f[i][j - 1], f[i - 1] ...
- 【读书笔记】iOS-更改编辑器键的绑定
一,Xcode-->Preferences--->Key Bindings. 参考资料:<Xcode实战开发>
- 判断字符串a和b是否互为旋转词
旋转词:把字符串str的任意部分移动到后面形成的新字符串叫做字符串str的旋转词. 比如abc的旋转词有 abc,acb,cba,... 判断str1和str2是否互为旋转词,其最优解可以是时间复杂度 ...
- Android为TV端助力 自定义通知栏
package com.example.mvp; import cn.ljuns.temperature.view.TemperatureView;import presenter.ILoginPre ...