【发布时间】:2019-06-04 16:06:45
【问题描述】:
我已经参考了下面列出的问题,但没有得到相关答案。
- How can I merge properties of two JavaScript objects dynamically?
- merge two json object based on key value in javascript
- Merge two array of objects based on a key
我有 2 个对象数组,
OBJ1 -
[
{
"ID": 58895,
"step": "Outage Agreed w/ Business"
},
{
"ID": 58896,
"step": "GMLC/E911/CMAS Significant"
}
]
OBJ2 -
[
{
"type": "verification_step",
"value": "GMLC/E911/CMAS Significant"
},
{
"type": "verification_step",
"value": "Outage Agreed w/ Business"
}
]
我希望输出包含基于字符串值的单个对象中的两个值,即
[
{
"ID": 58896,
"type": "verification_step",
"step": "GMLC/E911/CMAS Significant"
},
{
"ID": 58895,
"type": "verification_step",
"step": "Outage Agreed w/ Business"
}
]
请给我建议出路。 (ES6 解决方案 - 非常感谢)
编辑
第三个被指定为重复的参考不是场景。键应保持“step”,数据应合并。
【问题讨论】:
-
obj1和obj2中的step和value是否相等
-
OBJ1.map(item => ({ ...item, type: OBJ2.find(item2 => item2.value === item.step).type, }))
标签: javascript arrays object array-merge