js array methods

1、map()返回一个加工处理完成后的新数组。
[5, 12, 8, 130, 44].map(s=> s*2) //[10, 24, 16, 260, 88]
2、filter()是筛选数组,返回一个筛选后的新数组。
[5, 12, 8, 130, 44].filter(s=> s>=12) //[12, 130, 44]
3、find()返回数组中满足条件的第一个对象
[{'num':5,'name':'a'}, {'num':12,'name':'b'}, {num:8,name:'c'}, {num:130,name:"d"}, {num:44,name:'e'}].find(s=> s.name==='e') //{num:44,name:'e'}
4、findIndex()返回数组中满足条件的第一个对象的索引
[{'num':5,'name':'a'}, {'num':12,'name':'b'}, {num:8,name:'c'}, {num:130,name:"d"}, {num:44,name:'e'}].findIndex(s=> s.name==='e') //4
5、fill()将一个固定值替换数组的元素
array.fill(value, start, end)
[5, 12, 8, 130, 44].fill(1,1,4) //[5, 1, 1, 1, 44]
6、copyWithin()用于从数组的指定位置拷贝元素到数组的另一个指定位置中。
array.copyWithin(target, start, end)
复制数组的前面两个元素到第三和第四个位置上:
[5, 12, 8, 130, 44].copyWithin(2,0,2)//[5, 12, 5, 12, 44]
7、some()用于检测数组中的元素是否满足制定条件
[5, 12, 8, 130, 44].some(s=>s===10)//false
[5, 12, 8, 130, 44].some(s=>s===8)//true
8、includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
[5, 12, 8, 130, 44].includes(8)// true
9、every()方法用于检测数组所有元素是否都符合制定条件
[5, 12, 8, 130, 44].every(s=>s>10)//false
[5, 12, 8, 130, 44].every(s=>s>4)//true
10、reduce()方法接受一个函数作为累加器,数组中的每个值开始缩减,最终计算为一个值。
[5, 12, 8, 130, 44].reduce((x,y)=>x+y)//199

----------------------------------------------------------------other methods-----------------------------------------------------------------------

1、数组去重

  - for循环,拿数组第一个for循环进行对比不相等push到新的数组。新的数组即为去重过的数组。

  - new Set方法。[...new Set([1,2,3,4,5,3,2,1])]

 

posted @ 2020-09-28 13:35  温少昌  阅读(189)  评论(0编辑  收藏  举报