JS过滤器数组根据条件删除重复的值

时间:2018-11-18 12:20:16

标签: javascript arrays

我有一个包含重复元素网的数组

let myArray=[
     {role: "role-1", deviceId: ""},
     {role: "role-2", deviceId: "d-2"},
     {role: "role-3", deviceId: "d-3"},
     {role: "role-1", deviceId: "d-1"},
     {role: "role-2", deviceId: ""},
     {role: "role-4", deviceId: ""}
     {role: "role-5", deviceId: ""}
]

我想删除重复的角色,并拥有包含不带Empty(“”)deviceIds的角色的数组,如果deviceId为空,则仅保留一个没有重复的角色

myArray=[
         {role: "role-1", deviceId: "d-1"},
         {role: "role-2", deviceId: "d-2"},
         {role: "role-3", deviceId: "d-3"}
         {role: "role-4", deviceId: ""}
         {role: "role-5", deviceId: ""}

 ]

我已经以这种方式编写了函数

function dedupeByKey(arr, key) {
  const temp = arr.map(el => el[key]);
  return arr.filter((el, i) =>
    temp.indexOf(el[key]) === i
  );
}

console.log(dedupeByKey(myArray, 'role'));

但是结果是,它没有检查是否为具有值的deviceId和具有空deviceId的角色赋予优先级。该如何解决?

4 个答案:

答案 0 :(得分:1)

您可以按照以下代码片段将唯一角色映射到对象,并将该对象简化为数组

let myArray = [
     {role: "role-1", deviceId: ""},
     {role: "role-2", deviceId: "d-2"},
     {role: "role-3", deviceId: "d-3"},
     {role: "role-1", deviceId: "d-1"},
     {role: "role-2", deviceId: ""},
     {role: "role-4", deviceId: ""},
     {role: "role-5", deviceId: ""}
];

var uniqueObj = myArray.reduce(function(acc, item) {
  var deviceId = acc[item.role] && acc[item.role].deviceId || item.deviceId;
  acc[item.role] = item;
  acc[item.role].deviceId = deviceId;
  return acc;
}, {});

var result = Object.keys(uniqueObj).reduce(function(acc2, item) {
  acc2.push(uniqueObj[item]);
  return acc2;
}, []);

console.log(result);

答案 1 :(得分:1)

您可以对数组中的重复项应用过滤器,以查找重复项,以决定过滤索引还是保留索引

const myArray= [
     {role: "role-1", deviceId: ""},
     {role: "role-2", deviceId: ""},
     {role: "role-3", deviceId: "d-3"},
     {role: "role-1", deviceId: "d-1"},
     {role: "role-2", deviceId: ""},
     {role: "role-4", deviceId: ""},
     {role: "role-5", deviceId: ""}
]
  
const cleanArray = myArray.filter( (item,index,array) => {
  if ( item.deviceId === "") {
    // filter it out when the same role is found in the array and the index isn't the same as current item you are looking at 
    return !array.some((i,idx) => i.role === item.role && idx > index  )
  }
  return true 
})

// for presentation: sort the array
const sortedArray = cleanArray.sort( (curr, next) => curr.role >  next.role? 1:-1);

console.log(sortedArray)

答案 2 :(得分:1)

您可以在默认情况下将reduce用作object,并且如果需要,可以将其最后转换为array。

let myArray = [
     {role: "role-1", deviceId: ""},
     {role: "role-2", deviceId: "d-2"},
     {role: "role-3", deviceId: "d-3"},
     {role: "role-1", deviceId: "d-1"},
     {role: "role-2", deviceId: ""},
     {role: "role-4", deviceId: ""},
     {role: "role-5", deviceId: ""}
]

const res = myArray.reduce((agg, itr) => {
  if (agg[itr.role]) return agg // if deviceId already exist, skip this iteration
  agg[itr.role] = itr.deviceId  // if deviceId not exist, Add it
  return agg
}, {})

let make_array = Object.keys(res).map(key => { return { role: key, deviceId: res[key] }})

console.log(make_array)

答案 3 :(得分:0)

我将按role分组,然后用deviceId进行第一个分组:

  function groupBy(array, key) {
    const result = { };
    for(const el of array) {
      if(!result[ el[key] ]) result[ el[key] ] = [];
       result[ el[key] ].push(el);
   }
  return result;
}

const result = [];
const grouped = groupBy(myArray, "role");
for(const group of Object.values(grouped)) {
  result.push(group.find(it => it.deviceId) || group[0]);
}