2016-12-30 133 views
2

在我的代码中,我创建了一个名为array1的数组。在这个数组中,我列出了多个对象。我想过滤掉array1对象的值是唯一的,并且需要用它们各自的值对id进行分组。我加入这里我的代码,如何从javascript数组中删除重复的对象?

数组1

var array1 = [ 
      { 
       value:"A", 
       id:1 
      }, 
      { 
       value:"B", 
       id:1 
      }, 
      { 
       value:"C", 
       id:2 
      }, 
      { 
       value:"B", 
       id:5 
      }, 
      { 
       value:"A", 
       id:2 
      }, 
      { 
       value:"A", 
       id:1 
      } 
     ]; 

这是我想要的结果,

[ 
      { 
       group:"A", 
       groupIds:[1, 2] 
      }, 
      { 
       group:"B", 
       groupIds:[1, 5] 
      }, 
      { 
       group:"C", 
       groupIds:[2] 
      } 
     ] 
+2

你有搜寻SO?有一些类似的问题:http://stackoverflow.com/questions/24919074/js-group-array-values-by-groups,http://stackoverflow.com/questions/12873228/javascript-group-by-array,http ://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects –

+0

我搜索并尝试过。但我在我的问题中添加的结果不是像那样 – Sathya

回答

2

您可以用数值来forEach()组对象和Set()groupIds

var array1 = [{"value":"A","id":1},{"value":"B","id":1},{"value":"C","id":2},{"value":"B","id":5},{"value":"A","id":2},{"value":"A","id":1}] 
 

 
var result = []; 
 
array1.forEach(function(e) { 
 
    if(!this[e.value]) (this[e.value] = {group: e.value, groupIds: []}) && result.push(this[e.value]) 
 
    this[e.value].groupIds = [...new Set(this[e.value].groupIds.concat(e.id))] 
 
}, {}) 
 

 
console.log(result)

+0

谢谢.. @nenad。 – Sathya

+0

不客气。 –

4

在普通的JavaScript,你可以使用一个哈希表和Array#indexOf唯一值。

var array = [{ value: "A", id: 1 }, { value: "B", id: 1 }, { value: "C", id: 2 }, { value: "B", id: 5 }, { value: "A", id: 2 }, { value: "A", id: 1 }], 
 
    grouped = array.reduce(function (hash) { 
 
     return function (r, a) { 
 
      if (!hash[a.value]) { 
 
       hash[a.value] = { group: a.value, groupIds: [] }; 
 
       r.push(hash[a.value]); 
 
      } 
 
      if (hash[a.value].groupIds.indexOf(a.id) === -1) { 
 
       hash[a.value].groupIds.push(a.id); 
 
      } 
 
      return r; 
 
     }; 
 
    }(Object.create(null)), []); 
 

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

0

使用_.groupBy和删除重复的ID迭代对象的值

var res = _.chain(array1) 
    .groupBy('value') 
    .mapValues(function(val, key) { 
     return { 
      group: key, 
      groupIds: _.chain(val).map('id').uniq().value() 
     }; 
    }) 
    .values() 
    .value(); 
0

这里还有一个在普通的JavaScript。

var hash = {} 
array1.forEach(function(e) { 
    hash[e.value] = hash[e.value] || []; 
    if (hash[e.value].indexOf(e.id) == -1) { hash[e.value].push(e.id) } 
}) 
var result = Object.keys(hash).map(function(key) { 
    return { group: key, groupIds: hash[key] } 
}) 
0

使用lodash:

_(array1) 
    .uniqWith(_.isEqual) 
    .groupBy('value') 
    .map((v, k) => ({ group: k, groupIds: _.map(v, 'id')})) 
    .value() 
  • uniqWith()使用isEqual()删除重复。你想要使用这种方法来比较idvalue道具。
  • groupBy()创建一个对象,其键是value属性。由于初始数组中有三个唯一值,因此此对象应具有三个键。
  • map()将对象转回到数组中,并具有预期的groupgroupIds属性。