数组对象去重 可以根据一个或者多个字段去重

数组对象去重 可以根据一个或者多个字段去重

var b = [
    { scac: "0CMDU", routeCode: "JADE EXPRESS", carrier: "CMA", disPlayName: "VALUES" },
    { scac: "0CMDU", routeCode: "JADE EXPRESS", carrier: "CMA", disPlayName: "VALUE" },
    { scac: "APLU", routeCode: "South China - Loop 8 (SC8)", carrier: "APL", disPlayName: "VALUE" },
    { scac: "APLU", routeCode: "South China - Loop 8 (SC8)", carrier: "APL", disPlayName: "VALUE" },
    { scac: "ANNU", routeCode: "JDX", carrier: "ANL", disPlayName: "VALUE" },
    { scac: "ANNU", routeCode: "JDX", carrier: "ANL", disPlayName: "VALUE" },
    { scac: "APLU", routeCode: "South China - Loop 8 (SC8)", carrier: "APL", disPlayName: "VALUE" },
    { scac: "CMDU", routeCode: "JADE EXPRESS", carrier: "CMA", disPlayName: "VALUE" },
    { scac: "CMDU", routeCode: "JADE EXPRESS", carrier: "CMA", disPlayName: "VALUE" },
    { scac: "CMDU", routeCode: "JADE EXPRESS", carrier: "CMA", disPlayName: "VALUE" },
  ]
  dupRemoval(b)
  function dupRemoval(arr) { //arr是传入的数组
    var nn = [...arr]
    let obj = {};
    let peon = nn.reduce((cur, next) => {
      //根据 属性scac + 属性disPlayName 判断去重
      obj[next.scac + next.disPlayName] ? "" : obj[next.scac + next.disPlayName] = true && cur.push(next);
      return cur;
    }, []) //设置cur默认类型为数组,并且初始值为空的数组
    console.log(peon)
    return peon
  }
发布了17 篇原创文章 · 获赞 0 · 访问量 199

猜你喜欢

转载自blog.csdn.net/who_become_gods/article/details/104498176