使用Lotash合并单个数组中的重复对象 [英] Merge duplicate objects in single array using lodash

查看:40
本文介绍了使用Lotash合并单个数组中的重复对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并收到的json数组中的重复对象。

数组如下所示:

{
  modules: [{
    "name": "Weazel",
    "otherprop": ["a", "b"]
  }, {
    "name": "weazel",
    "otherprop": ["c", "b"]
  }]
}

由于某些原因,我想不出如何合并重复项。

我尝试过先将所有名称映射为小写,然后使用Unique,但这样会删除其他属性的值。

let result = _.map(json.modules, mod => { mod.name = mod.name.tolower(); return mod; });
result = _.unique(result, 'name');

有人知道如何使用Lotash来撞击我的问题吗?

推荐答案

var result = _.uniq(json.modules, function(item, key, a) { 
        return item.a;
    });

//Result : [{"name":"Weazel","otherprop":["a","b"]}]

这篇关于使用Lotash合并单个数组中的重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆