【发布时间】:2016-12-28 00:44:26
【问题描述】:
我有两个对象数组weather 和power,我想在共享属性date 上合并它们。有没有办法用 lodash 做到这一点?
const weather = [
{
date: 2,
weather: 2
},
{
date: 1,
weather: 1
},
{
date: 3,
weather: 3
}
];
const power = [
{
date: 1,
power: 10
},
{
date: 2,
power: 20
}];
const merged = _.merge(weather, power); // <- This does not work as hoped for
预期的输出仅包含具有匹配 date 字段的对象(因此删除了 weather 数组中的 date: 3 对象)。
const expected = [{
date: 1,
weather: 1,
power: 10
},
{
date: 2,
weather: 2,
power: 20
}];
我觉得 union、unionBy、merge 或类似的应该可以做到这一点,但我在文档中找不到匹配的示例。
【问题讨论】:
标签: javascript lodash