数组常用的方法

倒叙循环

for (let i = a.length-1; i >= 0; i--) {
      if(a[i]===3){
        console.log(i);
        a.splice(i,1);
      }
 }
console.log(a);//[0]

数组降维

const deps = {
    '采购部':[1,2,3],
    '人事部':[5,8,12],
    '行政部':[5,14,79],
    '运输部':[3,64,105],
}
let member = Object.values(deps).flat(Infinity); // [1, 2, 3, 5, 8, 12, 5, 14, 79, 3, 64, 105]

数组去重

let hash = {}; 
data = data.reduce((preVal, curVal) => {
    hash[curVal.id] ? '' : hash[curVal.id] = true && preVal.push(curVal); 
    return preVal 
}, [])

数组筛选

    const array = JSON.parse(JSON.stringify(this.groupServiceListSearch));
    this.groupServiceList = array.filter(item => {
      item.groupServices = item.groupServices.filter(itemChild => {
        itemChild.items = itemChild.items.filter(res => res.name.includes(childName));
        return itemChild.items?.length > 0;
      });
      return item.groupServices?.length > 0;
    });

 

posted @ 2022-04-11 19:10  宇智波copy  阅读(30)  评论(0)    收藏  举报