数组的方法

添加和删除:添加(push、unshift),删除(pop、shift).

在数组任意位置插入或删除:splice(index,nums,item1,item2、、) 第一个参数表示从什么位置开始,第二个参数表示对几个数进行调整,后边没参数表示没有插入只删除,有参数表示有插入和删除。   返回值:删除或添加的数组。

遍历的方法:

forEach:对每个元素执行回调函数。

map:创建一个全新的数组,遍历输入的数组。在新数组上创建每个回调函数返回的结果。

处理集合的方法:

some:接受回调函数作为参数,对每个元素执行回调函数,如果回调结果至少有一项返回true,some返回true。

every:接受回调函数作为参数,对每个元素执行回调函数,如果回调结果都返回true,every返回true。

1     const arr=[{name:"yangyu",weapon:"shuriken"},{name:"yoshi",weapon:"katana"},{name:'kuma',weapon:"waki"}];
2     const arr1=arr.every(item=>"name" in item);
3     console.log(arr1);  //true

数组查找的方法:

find:在数组中查找元素,返回第一个回调函数返回true的元素。

filter:在数组中查找元素,返回一个数组,包含回调函数返回true的全部元素。

indexOf:查找特定元素的索引,传入目标元素作为参数。

lastIndexOf:查找最后一次出现的索引。

findIndex:在数组中查找元素,返回第一个回调函数返回true的元素索引。

数组排序方法:

sort: 提供比较回调函数。

reduce:遍历集合中元素求和。

猜你喜欢

转载自www.cnblogs.com/alaner/p/9549108.html