【发布时间】:2016-04-02 13:29:31
【问题描述】:
我有一个数组,其中包含许多几乎相同的对象。我想将这些对象合并为一个,同时保持它们之间不一样的数据。
这是我的数据:
[
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 1, name: 'Cat 1'}
]
},
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 2, name: 'Cat 2'}
]
}
]
我希望最终结果是:
[
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 1, name: 'Cat 1'},
{id: 2, name: 'Cat 2'}
]
}
]
任何帮助将不胜感激!
【问题讨论】:
-
试试
_.mergeWith()的例子。 -
Stackoverflow 不是您粘贴数据和所需结果并获得解决方案的地方。我们在这里为您提供帮助,而不是为您考虑。
-
“几乎相同”的定义是什么?
-
到目前为止,我已经尝试过 _.uniq、_uniqWith、_.union、_.unionWith 以及它们的组合。谢谢你,Aristarhys 的有用评论。随着我更多地使用 Stackflow,我希望我有机会回报这个人情。
-
谢谢你,乔纳森!我认为这对我有用。感谢您的帮助。
标签: javascript arrays node.js lodash