根据不同的键但相同的值合并 2 个对象数组
Merge 2 arrays of objects based on different key but same value
我参考了下面列出的问题,但没有得到相关的答案。
- How can I merge properties of two JavaScript objects dynamically?
我有 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",数据应合并。
将两个数组合并后就可以使用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))
因为你需要ES6,你可以只使用map操作符,将缺失值连接到just on object。
像这样
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
}
});
可能有打字错误,我只是打字,但你明白了。
您需要做必要的检查以避免未定义。
你可以写
这是用于测试 online es6 compiler
的 link
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
另一个数组可能会更好数组使用查找加入你想要的其他值。类似于:
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)
您可以为 ID
使用 Map
并获取此值以映射新对象。
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);
您可以使用 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()
我参考了下面列出的问题,但没有得到相关的答案。
- How can I merge properties of two JavaScript objects dynamically?
我有 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",数据应合并。
将两个数组合并后就可以使用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))
因为你需要ES6,你可以只使用map操作符,将缺失值连接到just on object。 像这样
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
}
});
可能有打字错误,我只是打字,但你明白了。 您需要做必要的检查以避免未定义。
你可以写 这是用于测试 online es6 compiler
的 linklet 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
另一个数组可能会更好数组使用查找加入你想要的其他值。类似于:
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)
您可以为 ID
使用 Map
并获取此值以映射新对象。
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);
您可以使用 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()