[英]Merge 2 arrays of objects based on different key but same value
我已经提到了下面列出的问题,但没有得到相关的答案。
我有 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 解决方案 - 非常感谢)
编辑
已被指定为重复的第三个参考不是场景。 关键应该保持“步骤”,数据应该合并。
您可以在组合两个数组后使用reduce()
。
const arr1 = [ { "ID": 58895, "step": "Outage Agreed w/ Business" }, { "ID": 58896, "step": "GMLC/E911/CMAS Significant" } ] const arr2 = [ { "type": "verification_step", "value": "GMLC/E911/CMAS Significant" }, { "type": "verification_step", "value": "Outage Agreed w/ Business" } ] const res = [...arr1,...arr2.map(({value,...rest}) => ({step:value,...rest}))].reduce((ac,a) => { let k = a.step; ac[k] = {...ac[k],...a} || a; return ac; },{}) console.log(Object.values(res))
您可能可以将其作为单行代码来执行,但为了可读性和效率,根据您想要从一个数组中的值创建一个查找对象,然后使用查找map
另一个数组以连接另一个值可能会更好你要。 就像是:
let arr1 = [{"ID": 58895,"step": "Outage Agreed w/ Business"},{"ID": 58896,"step": "GMLC/E911/CMAS Significant"}] let arr2 = [{"type": "verification_step","value": "GMLC/E911/CMAS Significant"},{"type": "verification_step","value": "Outage Agreed w/ Business"}] // lookup based on value let lookup = arr2.reduce((m, {type, value}) => m.set(value, {type}), new Map) // merge each item based on lookup let result = arr1.map(item => Object.assign({}, item, lookup.get(item.step))) console.log(result)
您可以使用lodash :
const _ = require('lodash');
const arr1 = [
{
"ID": 58895,
"step": "Outage Agreed w/ Business"
},
{
"ID": 58896,
"step": "GMLC/E911/CMAS Significant"
}
]
const arr2 = [
{
"type": "verification_step",
"value": "GMLC/E911/CMAS Significant"
},
{
"type": "verification_step",
"value": "Outage Agreed w/ Business"
}
]
const res = _(arr1)
.keyBy('step')
.merge((_.keyBy(arr2, 'value')))
.values()
.map((value) => {
const { ID, type, step } = value
return {
ID,
type,
step
}
})
.value()
由于您需要 ES6,您可以只使用 map 运算符并将缺失值连接到对象上。 像这样
let obj=[
{
"type": "verification_step",
"value": "GMLC/E911/CMAS Significant"
},
{
"type": "verification_step",
"value": "Outage Agreed w/ Business"
}
]
const obj1=[
{
"ID": 58895,
"step": "Outage Agreed w/ Business"
},
{
"ID": 58896,
"step": "GMLC/E911/CMAS Significant"
}
]
obj.map((ob)=>{
const found=obj1.find((e)=>{
return e.step===ob.value;
});
if(found){
ob.ID=found.ID
}
});
可能有错别字,我只是输入了它,但你明白了。 您需要进行必要的检查以避免未定义。
你可以只写这里是测试在线es6编译器的链接
let arr = [];
obj1.map((val, index) => {
if(obj2[index]) {
arr.push({ id: val.ID, type: obj2[index].type, value: obj2[index].value })
}
});
console.log(arr);
您可以使用Map
作为ID
并获取此值以映射新对象。
var array1 = [{ ID: 58895, step: "Outage Agreed w/ Business" }, { ID: 58896, step: "GMLC/E911/CMAS Significant" }], array2 = [{ type: "verification_step", value: "GMLC/E911/CMAS Significant" }, { type: "verification_step", value: "Outage Agreed w/ Business" }], map = new Map(array1.map(({ ID, step }) => [step, ID])), result = array2.map(({ type, value: step }) => ({ ID: map.get(step), type, step })); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.