es6reduce实现数组对象合并去重

2022-04-13  本文已影响0人  Morbid_D

js版本

let pesonList = [{id:0,name:'111'},{id:0,name:'111'},{id:1,name:'111'},{id:0,name:'111'},]]

let obj = {} cur = []

let pes = pesonList.reduce((cur,next) => {

          obj[next.id] ? "" : obj[next.id] = true && cur.push(next);

          return cur;

      },[])

ts封装版本

/**

 * ArrorSet

 * 数组对象去重

 * arr数组数据,setType需要筛选的字段

 */

const ArrorSet = (arr: any, setType: any) => {

  console.log('arr', arr, setType);

  let obj: any = {};

  let ArrorSetList = arr.reduce((cur: any, next: any) => {

    obj[next[setType]] ? '' : (obj[next[setType]] = true && cur.push(next));

    return cur;

  }, []);

  return ArrorSetList;

};

上一篇 下一篇

猜你喜欢

热点阅读