js 数组操作常用方法
code参考了: https://www.cnblogs.com/woshidouzia/p/9304603.html
//测试数组
let arr = [1,2,3,4,5,6,7]
console.log("=============1.for循环=============")
for(let j=0, len = arr.length; j<len;j++){
console.log(arr[j])
}
console.log("=============2.forEach循环=============")
arr.forEach((item,index,array)=>{
console.log(item,index,array)
})
console.log("=============3.map循环=============")
//map的回调函数中支持return返回值;return的是啥,相当于把数组中的这一项变为啥
// (并不影响原来的数组,只是相当于把原数组克隆一份,把克隆的这一份的数组中的对应项改变了);
arr.map(function(value,index,array){
console.log("map循环"+index+array)
return 2*value;
})
console.log("=============4.forof遍历=============")
for (var value of arr){
console.log("forof遍历"+value)
}
console.log("=============5.filter遍历=============")
var arr1 = [
{ id: 1, text: 'aa', done: true },
{ id: 2, text: 'bb', done: false }
]
console.log(arr1.filter(item => item.done))
console.log("=============6.every遍历=============")
//every()是对数组中的每一项运行给定函数,如果该函数对每一项返回true,则返回true。
var arr2 = [ 1, 2, 3, 4, 5, 6 ];
console.log( arr2.every( function( item, index, array ){
console.log("every遍历" + index + array)
return item > 3;
}));
console.log("=============7.some遍历=============")
//some()是对数组中每一项运行指定函数,如果该函数对任一项返回true,则返回true。
var arr3 = [ 1, 2, 3, 4, 5, 6 ];
console.log( arr3.some( function( item, index, array ){
console.log("some遍历" + index + array)
return item > 3;
}));
console.log("=============8.reduce() =============")
//reduce()方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值。
arr.reduce(function(previousValue, currentValue, index, array){
console.log("reduce遍历" + index + array)
return previousValue + currentValue;
});
console.log("=============9.reduce 还有第二个参数=============")
//我们可以把这个参数作为第一次调用callback时的第一个参数,上面这个例子因为没有第二个参数,所以直接从数组的第二项开始,如果我们给了第二个参数为5,那么结果就是这样的:
arr.reduce(function(previousValue, currentValue, index, array){
console.log("reduce遍历" + index + array)
return previousValue + currentValue;
},5);
console.log("=============10.reduceRight()=============")
//方法的功能和reduce()功能是一样的,不同的是reduceRight()从数组的末尾向前将数组中的数组项做累加。
var arr4 = [0,1,2,3,4];
arr4.reduceRight(function (preValue,curValue,index,array) {
console.log("reduceRight"+index+array)
return preValue + curValue;
});
console.log("=============11.find=============")
//find()方法返回数组中符合测试函数条件的第一个元素。否则返回undefined
var stu = [
{
name: '张三',
gender: '男',
age: 20
},
{
name: '王小毛',
gender: '男',
age: 20
},
{
name: '李四',
gender: '男',
age: 20
}
]
console.log("find"+stu.find((element) => (element.name == '李四')))
console.log("=============12.findIndex=============")
//对于数组中的每个元素,findIndex 方法都会调用一次回调函数(采用升序索引顺序),直到有元素返回 true。只要有一个元素返回 true,findIndex 立即返回该返回 true 的元素的索引值。
// 如果数组中没有任何元素返回 true,则 findIndex 返回 -1。
var arr5 = [1,2,3]
console.log(arr5.findIndex(function(x) { x == 2; }))
console.log(arr5.findIndex(x => x == 4))
console.log("=============keys,values,entries=============")
//ES6 提供三个新的方法 —— entries(),keys()和values() —— 用于遍历数组。
// 它们都返回一个遍历器对象,可以用for...of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935.
我的gitHub: (学习代码都在gitHub)
https://github.com/nwgdegitHub/

浙公网安备 33010602011771号