通过键名从对象数组中删除重复的对象

时间:2018-12-07 10:24:04

标签: javascript arrays json loops object

我有以下对象数组:

var arr = [
  {name: 'rajendra', last: 'arora'},
  {name: 'rajendra', last: 'arora2'},
  {name: 'rajendra1', last: 'arora22'},
  {name: 'rajendra2', last: 'arora233'},
  {name: 'rajendra2', last: 'arora23'},
  {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendr3', last: 'arora3'}
]

我想通过键名name删除重复的对象。我的预期输出:

var arr = [
  {name: 'rajendra', last: 'arora'},
  {name: 'rajendra1', last: 'arora22'},
  {name: 'rajendra2', last: 'arora233'},
  {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendr3', last: 'arora3'}
]

我尝试过以下程序:

function _unique(arr) {
  let uniqueArr = [];

  for (var i = 0; i < arr.length; i++) {
    for(var j=i+1; j<arr.length; j++) {
      if(arr[i].name.indexOf(arr[j].name) == -1) {
        uniqueArr.push(arr[j])
      }
    }
  }
  // console.log(uniqueArr[0].name.indexOf("rajendra"))
  return uniqueArr;
}

console.log(_unique(arr))

但是它不起作用。

将获得帮助!

4 个答案:

答案 0 :(得分:2)

reduce放入由name索引的对象中,仅当该属性中不存在任何项目时才分配给该属性,然后获取该对象的值:

var arr = [
  {name: 'rajendra', last: 'arora'},
  {name: 'rajendra', last: 'arora2'},
  {name: 'rajendra1', last: 'arora22'},
  {name: 'rajendra2', last: 'arora233'},
  {name: 'rajendra2', last: 'arora23'},
  {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendr3', last: 'arora3'}
];
const output = Object.values(arr.reduce((a, item) => {
  if (!a[item.name]) a[item.name] = item;
  return a;
}, {}));
console.log(output);

答案 1 :(得分:1)

var arr = [
  {name: 'rajendra', last: 'arora'},
  {name: 'rajendra', last: 'arora2'},
  {name: 'rajendra1', last: 'arora22'},
  {name: 'rajendra2', last: 'arora233'},
  {name: 'rajendra2', last: 'arora23'},
  {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendr3', last: 'arora3'}
];

var resultArr = arr
  .sort((a, b) => a.name-b.name )
  .reduce((accu, curr, i) => {
    let length = accu.length;
    if(length == 0 || accu[length - 1].name !== curr.name) {
      accu.push(curr);
    }
    return accu;
  }, []);

console.log(resultArr);

答案 2 :(得分:0)

您可以使用.reduce()Object.values()来获得结果数组:

let data = [
  {name: 'rajendra', last: 'arora'},    {name: 'rajendra', last: 'arora2'},
  {name: 'rajendra1', last: 'arora22'}, {name: 'rajendra2', last: 'arora233'},
  {name: 'rajendra2', last: 'arora23'}, {name: 'rajendra3', last: 'arora3'},
  {name: 'rajendra3', last: 'arora3'},  {name: 'rajendr3', last: 'arora3'}
];

let result = Object.values(data.reduce((r, c) => (r[c.name] = r[c.name] || c, r), {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

我对您现有的解决方案进行了少量修改:

function _unique(arr) {
  let uniqueArr = [];

  for (var i = 0; i < arr.length; i++) {
    let existed = false
    for (var j = 0; j < uniqueArr.length; j++) 
      if (arr[i].name === uniqueArr[j].name) {
        existed = true
        break
      }

    if (existed) continue
    uniqueArr.push(arr[i])
  }

  return uniqueArr;
}