1.数组对象
属性和方法
var arr = [10,20, 30]arr.length属性
方法:
1-push
=> arr. push()
=>向数组追加元素
2. pop
=>arr. pop()
=>删除数组后面元素
3. unshift
=>arr.unshift
=>数组前面添加元素
4. shift
=>arr.shift//删除数组最前面元素
5.splice(下标,元素个数,替换元素)
=> arr.splice(1,2 )
6. revers
=>反转数组元素 [1,2,3] -> [3,2,1]
2.数组遍历方法
1. forEach
匿名函数
function(){}
arr.forEach(function(item,index){
// 第一个形参: 数组元素
// 第二个形参: index 索引号(下标)
})
2. map
var newArr = arr.map(function(item,index){
return 新元素
})
[新元素1,新元素2,...]
3. filter
var newArr = arr.filter(function(item,index){
return 条件
})
查找数组满足条件的元素,返回新数组
4. find
查找第一个满足条件的元素
5. every
遍历数组,查找数组元素是否全部满足条件,全部满足返回真
6. some
遍历数组,查找数组是否有一个满足条件,有一个满足条件返回真
7.reduce
reduct(callFun, [initValue])
callFun: function(previousValue , currentValue , currentIndex , arr){
}
forEach(function(currentValue , currentindex , arr){
})
previousValue 上一个值,如果initValue存在,第一遍历时previousValue就是initValue
currentValue 当前数组元素
currentIndex 当前数组下标
arr 原数组