[英]How can I create a merged object from an array of objects that preserves properties in the original object not changed from the new incoming object
我必须对象/数组集...
var test = [{id: 1, prop: 'bill', location: 'san diego'},{id: 11, prop: 'bright'}];
var test2 = [{id: 1, prop: 'casey'},{id: 11, prop: 'candle'}];
最终,我希望test2中的对象更改test中的属性,但将位置属性保留在原始对象中,因为它没有更改,甚至没有在test2中引用。
我所做的是设置一个映射,该映射的键是id,并使用每个对象作为值。
出现问题,每个键的新对象的值将覆盖整个先前的原始对象。 因此,location属性将从原始数组中删除。
值得注意的是,从Object.assign()开始,未作为对象数组呈现的裸对象将保留原始对象的完整性,并且仅更改基础属性。
这是两个示例:
对象数组:
var o1 = [{id: 1, prop: 'bill', location: 'san diego'},{id: 11, prop: 'bright'}];
var o2 = [{id: 1, prop: 'casey'},{id: 11, prop: 'candle'}];
var o3 = { c: 3 };
var obj = Object.assign({}, o1, o2, o3);
console.log(obj); // 0: {id: 1, prop: "casey"} 1: {id: 11, prop: "candle"} c: 3
单个对象:
var o1 = {id: 1, prop: 'bill', location: 'san diego'};
var o2 = {id: 1, prop: 'casey'};
var o3 = { c: 3 };
var obj = Object.assign({}, o1, o2, o3);
console.log(obj); // {id: 1, prop: "casey", location: "san diego", c: 3}
以单一格式处理对象具有预期的效果,因此答案可能就在于此。 是否有一种简洁的方法可以做到这一点,还是需要以循环方式建立一次处理一次记录的方式? 如果是这样,这将是一个例子吗?
这是我正在处理的映射代码:
var test = [{id: 1, prop: 'bill', location: 'san diego'},{id: 11, prop: 'bright'}];
var first = new Map();
for (const value of test) {
//console.log(key + ' = ' + value.id);
first.set(value.id, value);
//console.log(first);
}
var test2 = [{id: 1, prop: 'casey'},{id: 11, prop: 'candle'}];
var second = new Map();
for (const value of test2) {
second.set(value.id, value);
}
var merged = new Map([...first, ...second]);
let mergedArray = [];
for (var value of merged.values()) {
console.log(' = ' + value);
mergedArray.push(value);
}
console.log('merged', mergedArray);
// console.log(merged)
// console.log('second2', second);
// console.log('first2 ', first);
const merged = test.map(ele => {
const found = test2.find(({id}) => id === ele.id) || {}
return {...ele, ...found}
})
test
test2
找到相同的id元素(如果未找到,请设置一个空对象) test
是源,而test2
包含覆盖/添加 尝试这样的事情。
const test = [{ id: 1, prop: 'bill', location: 'san diego' }, { id: 11, prop: 'bright' }, { id: 12, //not in test2 prop: 'something', location: 'NY' }, { id: 13, //no "prop" in test2 prop: 'something', location: 'NY' } ]; const test2 = [{ id: 1, prop: 'casey' }, { id: '11', prop: 'candle' }, { id: 13, something: false }, { id: 100, // not in test prop: 'other' } ]; test.forEach(t => { const t2 = test2.find(t2 => t2.id == t.id);//=== for exact match if (t2) t.prop = t2.prop || t.prop; }); console.log(test);
通过使用Map()函数提供此答案,以便可以首先将对象设置为键值。 可接受的答案简短明了,但这可能会有附加价值。
var test = [{id: 1, prop: 'bill', location: 'san diego'},{id: 11, prop: 'bright'}];
var test2 = [{id: 1, prop: 'casey'},{id: 11, prop: 'candle'}];
const map = new Map(test.map(obj => [obj.id, obj]));
for (const item of test2) {
if (map.has(item.id)) {
// map.set( item.id, item ); // replace the item value
map.set(item.id, { ...map.get(item.id),
...item
});
}
}
console.log(Array.from(map));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.