【问题标题】:How to remove duplicates objects from array in javascript?如何从javascript中的数组中删除重复的对象?
【发布时间】:2016-12-30 11:56:07
【问题描述】:

在我的代码中,我创建了一个名为 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
            }
        ];

我想要的结果,

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

【问题讨论】:

标签: javascript lodash


【解决方案1】:

在纯 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】:

    您可以使用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)

    【讨论】:

      【解决方案3】:
      _.uniqBy(objects, function (object) {
        return object.id;
      });
      

      会做的:)

      【讨论】:

        【解决方案4】:

        使用_.groupBy_.mapValues 迭代对象值

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

        【讨论】:

          【解决方案5】:

          这是另一个纯 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] } 
          })
          

          【讨论】:

            【解决方案6】:

            使用 lodash:

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

            【讨论】:

              猜你喜欢
              • 2018-08-15
              • 2021-12-30
              • 2016-09-24
              • 2014-06-25
              • 2021-12-07
              • 1970-01-01
              • 2018-01-08
              • 1970-01-01
              相关资源
              最近更新 更多