gpt4 book ai didi

javascript - 如何将具有相同属性的对象合并到一个数组中?

转载 作者:搜寻专家 更新时间:2023-10-30 21:15:12 25 4
gpt4 key购买 nike

我想将 2 个具有相同属性的对象合并到一个数组中。

以此为例:

object1 = {"id":1,
"name":name1,
"children":[{"id":2,"name":name2}]
};
object2 = {"id":3,
"name":name3,
"children":[{"id":4,"name":name4}]
};
object3 = {"id":1,
"name":name1,
"children":[{"id":6,"name":name6}]
};
var result = Object.assign(result,object1,object2,object3);

预期结果:

JSON.stringify([result]) =[
{"id":1,
"name":name1,
"children":[{"id":2,"name":name2},
{"id":6,"name":name6}]
},
{"id":3,
"name":name3,
"children":[{"id":4,"name":name4}]
}
]

实际结果:

JSON.stringify([result]) = [
{"id":3,
"name":name3,
"children":[{"id":4,"name":name4}]
}
]

似乎 Object.assign() 不是正确的方法...因为它会覆盖,我不希望它覆盖,我希望它们合并。有正确的方法吗?

最佳答案

像往常一样,Array.prototype.reduce 为类似的方法提供了良好的基础这个……

var obj1 = {
"id": 1,
"name": "name1",
"children": [{ "id": 2, "name": "name2" }]
};
var obj2 = {
"id": 3,
"name": "name3",
"children": [{ "id": 4, "name": "name4" }]
};
var obj3 = {
"id": 1,
"name": "name1",
"children": [{ "id": 6, "name": "name6" }]
};
// Expected result: [{
// "id": 1,
// "name": name1,
// "children": [
// { "id": 2, "name": "name2" },
// { "id": 6, "name": "name6" }
// ]
// }, {
// "id": 3,
// "name": "name3",
// "children": [{"id": 4, "name": "name4" }]
// }]

function mergeEquallyLabeledTypes(collector, type) {
var key = (type.name + '@' + type.id); // identity key.
var store = collector.store;
var storedType = store[key];
if (storedType) { // merge `children` of identically named types.
storedType.children = storedType.children.concat(type.children);
} else {
store[key] = type;
collector.list.push(type);
}
return collector;
}

var result = [obj1, obj2, obj3].reduce(mergeEquallyLabeledTypes, {

store: {},
list: []

}).list;

console.log('result : ', result);
.as-console-wrapper { max-height: 100%!important; top: 0; }

编辑笔记

在得知需要处理嵌套模式的需求变更后,我会将我最初提供的方法更改为通用解决方案。这不会那么困难,因为在数据结构中有一个普遍重复的模式。因此我只需要让已经存在的 reducer 函数自递归。在任何提供的列表上完成一个完整的减少循环后,将触发递归步骤......

var obj1 = {
"id": 1,
"name": "name1",
"children": [{ "id": 2, "name": "name2", "children": [{ "id": 8, "name": "name8" }] }]
};
var obj2 = {
"id": 3,
"name": "name3",
"children": [{ "id": 4, "name": "name4", "children": [{ "id": 9, "name": "name9" }] }]
};
var obj3 = {
"id": 1,
"name": "name1",
"children": [{ "id": 6, "name": "name6", "children": [{ "id": 10, "name": "name10" }] }]
};
var obj4 = {
"id": 3,
"name": "name3",
"children": [{ "id": 4, "name": "name4", "children": [{ "id": 11, "name": "name11" }] }]
};

function mergeEquallyLabeledTypesRecursively(collector, type, idx, list) {
var key = (type.name + '@' + type.id); // identity key.
var store = collector.store;
var storedType = store[key];
if (storedType) { // merge `children` of identically named types.
storedType.children = storedType.children.concat(type.children);
} else {
store[key] = type;
collector.list.push(type);
}
// take repetitive data patterns into account ...
if (idx >= (list.length - 1)) {
collector.list.forEach(function (type) {

// ... behave recursive, when appropriate.
if (type.children) {
type.children = type.children.reduce(mergeEquallyLabeledTypesRecursively, {

store: {},
list: []

}).list;
}
});
}
return collector;
}

var result = [obj1, obj2, obj3, obj4].reduce(mergeEquallyLabeledTypesRecursively, {

store: {},
list: []

}).list;

console.log('result : ', result);
.as-console-wrapper { max-height: 100%!important; top: 0; }

关于javascript - 如何将具有相同属性的对象合并到一个数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46971158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com