美文网首页
es6reduce实现数组对象合并去重

es6reduce实现数组对象合并去重

作者: Morbid_D | 来源:发表于2022-04-13 15:28 被阅读0次

    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;

    };

    相关文章

      网友评论

          本文标题:es6reduce实现数组对象合并去重

          本文链接:https://www.haomeiwen.com/subject/buitertx.html