1.indexOf()
返回数组中第一个找到的元素的位置,不存在返回-1
a1=[‘ab’,45,67]
var a2=a1.indexOf(‘ab’)
console.log(‘ab’)
2.for each
var a1=[4,5,6,7,8]
a1.forEach(function(item,index,arr){
console.log(item,index)
})
item代表的是当前元素值,
index代表当前元素索引
arr代表当前数组
3.map
都是对数组的每一项进行遍历
a1=[3,4,5,6] 生成a2=[6,5,10,12]
var a2=a1.map(function(item,index,arr){
return item2
})
*****************map与forEach区别
1.forEach只是遍历,map生成新的数组
2.map比foreach速度快
3.map可以链式操作
*4.filter
arr.filter(function (当前元素值,当前元素索引,数组对象){})
(1)返回新数组,不对原数组修改
(2)对返回"true"的结果过滤,false的结果过滤
a1=[10,30,40,50,60,80]提取出大于30的数据
*5.reduce
接收一个函数作为累集器,数组中的每一个值,从左到右开始缩减
最终计算为一个值
arr.reduce(function (初始值,当前元素值,当前元素索引,数组对象){},初始值(可省略))
a1=[1,2,3,4,5]
a1.reduce(function(total,currentvalue,index,arr){
console.log(total,currentvalue,index,arr)
})
6.some
判断数组中是否有满足条件的元素
(1)a1.some(function(当前元素值,当前元素索引,数组对象){})
(2)函数内部,返回true,找到了满足条件的元素,则循环结束
返回false,没找到,循环继续
(3)函数的执行次数不一定等与数组的长度
// a1=[10,30,60]
// a2=a1.some(function(item,index,arr){
// return item>20
// })
// console.log(a2); //true
7.every
检测数组中所有元素是否都满足条件
(1)a1.some(function(当前元素值,当前元素索引,数组对象){})
(2)有一项不满足就返回false,都满足才返回true
a1=[10,30,5,60]
a2=a1.every(function(item){
return item>0
})
console.log(a2); //true