如何在JavaScript中从数组中删除重复的对象?

3

在我的代码中,我创建了一个名为array1的数组。在这个数组中,我列出了多个对象。我想要筛选出array1对象的唯一值,并需要将id与其相应的值分组。我在这里添加了我的代码,

数组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
            }
        ];

the result which i want,

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

2
你在 Stack Overflow 上搜索过吗?有一些类似的问题:http://stackoverflow.com/questions/24919074/js-group-array-values-by-groups,https://dev59.com/JWcs5IYBdhLWcg3waTRD,https://dev59.com/uGYq5IYBdhLWcg3wfgnd - Ricardo Pontual
我已经搜索和尝试过了,但是我在问题中添加的结果并不像那样出现。 - Sathya
6个回答

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; }


2
_.uniqBy(objects, function (object) {
  return object.id;
});

that will do :)


2
你可以使用forEach()按值对对象进行分组,使用Set()groupIds中删除重复的id。

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)


1
使用lodash:
_(array1)
  .uniqWith(_.isEqual)
  .groupBy('value')
  .map((v, k) => ({ group: k, groupIds: _.map(v, 'id')}))
  .value()
  • uniqWith()使用isEqual()来删除重复项。您希望使用此方法以便比较idvalue属性。
  • groupBy()创建一个对象,其键是value属性。由于初始数组中有三个唯一值,因此该对象应具有三个键。
  • map()将对象转换回数组,具有预期的groupgroupIds属性。

0
使用_.groupBy_.mapValues来迭代对象值。
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] } 
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接