使用for循环
let arr = [{name:"lisa"},{name:"linda"},{name:"cindy"}...]
for(let i=0; i<arr.length; i++){
if(arr[i]==2){
arr.splice(i, 1);
i--;
}
}
使用 forEach 循环
data.result.forEach((item, index, array) => {
if (array[index].name === "lisa") {
item = array.splice(index, 1);
}
});
总结一下遍历字段的方式
1.for循环
使用临时变量缓存宽度,避免重复获取链表的宽度。 当领域较大时,优化效果会更加显着。
for(j = 0,len=arr.length; j < len; j++) {
}
2.foreach循环
遍历链表中的每一项。 没有返回值,对原字段没有影响。 不支持 IE。
//1 没有返回值
arr.forEach((item,index,array)=>{
//执行代码
})
//参数:item数组中的当前项, index当前项的索引, array原始数组;
//数组中有几项,那么传递进去的匿名回调函数就需要执行几次;
3.地图循环
有返回值jquery数组个数,可以返回
Map的回调函数支持return返回值; 返回的内容相当于将链表中的此项更改为某项(不影响原始链表,但相当于克隆了原始链表的副本,并更改了克隆副本链表中的对应项已经改变);
arr.map(function(value,index,array){
//do something
return XXX
})
var ary = [12,23,24,42,1];
var res = ary.map(function (item,index,ary ) {
return item*10;
})
console.log(res);//-->[120,230,240,420,10]; 原数组拷贝了一份,并进行了修改
console.log(ary);//-->[12,23,24,42,1]; 原数组并未发生变化
4.forof遍历
能够正确回应中断、继续和返回句子
for (var value of myArray) {
console.log(value);
}
5.过滤器遍历
原来的字段不会改变jquery数组个数,返回一个新的链表。
var arr = [
{ id: 1, text: 'aa', done: true },
{ id: 2, text: 'bb', done: false }
]
console.log(arr.filter(item => item.done))
6.每次穿越
every() 对链表中的每个项目运行给定的函数。 如果该函数对每个项目都返回 true,则它返回 true。
var arr = [ 1, 2, 3, 4, 5, 6 ];
console.log( arr.every( function( item, index, array ){
return item > 3;
}));
false
7.一些遍历
some() 对链表中的每个项目运行指定的函数。 如果该函数对任何项返回 true,则它返回 true。
var arr = [ 1, 2, 3, 4, 5, 6 ];
console.log( arr.some( function( item, index, array ){
return item > 3;
}));
true
8.减少
reduce() 方法接收一个函数作为累加器,并开始将数组中的每个值(从左到右)减少为单个值。
var total = [0,1,2,3,4].reduce((a, b)=>a + b); //10
reduce接受一个函数,函数有四个参数,分别是:上一次的值,当前值,当前值的索引,数组
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
reduce还有第二个参数。 我们可以在第一次调用callback时使用该参数作为第一个参数。 在上面的例子中,由于没有第二个参数,所以我们直接从字段的第二项开始。 如果我们给第二个参数是5,那么结果是这样的:
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},5);
第一次调用中的 previousValue 值被传入的第二个参数替换。
9.reduceRight
reduceRight()方法的功能与reduce()函数相同。 不同的是,reduceRight()从字段尾部向前累积链表中的字段项。
当reduceRight()第一次调用回调函数callbackfn时,prevValue和curValue可以是两个值之一。 如果使用initialValue 参数调用reduceRight(),则prevValue 等于initialValue,curValue 等于列表中的最后一个值。 如果未提供initialValue参数,则prevValue等于链表中的最后一个值,curValue等于链表中倒数第二个值。
var arr = [0,1,2,3,4];
arr.reduceRight(function (preValue,curValue,index,array) {
return preValue + curValue;
回调将被调用四次。 每次调用的参数和返回值如下:
如果您提供的初始值为 5:
var arr = [0,1,2,3,4];
arr.reduceRight(function (preValue,curValue,index,array) {
return preValue + curValue;
}, 5); // 15
类似地,您可以对链表求和或使用 reduceRight() 方法:
var arr = [1,2,3,4,5,6];
console.time("ruduceRight");
Array.prototype.ruduceRightSum = function (){
for (var i = 0; i < 10000; i++) {
return this.reduceRight (function (preValue, curValue) {
return preValue + curValue;
});
}
}
arr.ruduceRightSum();
console.log('最终的值:' + arr.ruduceSum()); // 21
console.timeEnd("ruduceRight"); // 5.725ms
10.查找
find() 方法返回链表中满足测试函数条件的第一个元素。 否则返回未定义
var stu = [
{
name: '张三',
gender: '男',
age: 20
},
{
name: '王小毛',
gender: '男',
age: 20
},
{
name: '李四',
gender: '男',
age: 20
}
]
function getStu(element){
return element.name == '李四'
}
stu.find(getStu)
//返回结果为
//{name: "李四", gender: "男", age: 20}
11.查找索引
对于链表中的每个元素,findIndex 方法都会调用回调函数(按相反的索引顺序),直到有元素返回 true。 只要有一个元素返回true,findIndex就立即返回返回true的元素的索引值。 如果链表中没有返回 true 的元素,则 findIndex 返回 -1。
findIndex 不会改变链表对象。
const nameArr=[
{id:1,userName:"zhaoqian",age:27},
{id:2,userName:"sunli",age:23},
{id:3,userName:"zhouwu",age:25 },
{id:4,userName:"zhengwang",age:21}];
/*满足条件,返回下标位置2*/
var i1=nameArr.findIndex((value)=>value.age==25);
console.log(i1);
12.键、值、条目
ES6 提供了三个新方法——entries()、keys() 和 value()——用于遍历链表。它们都返回一个遍历器对象,可以使用 for...of 循环来遍历该对象。 唯一的区别是keys()遍历键值,values()遍历通配符,entries()遍历通配符对。
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"