我试图合并两个对象,它们都具有一些相同的键/关联数组和不同的键。关联的数组的长度也不同。为了给您更好的图片,我在下面添加了一些代码,这些代码显示了我要合并的两个对象(filters1和filters2)以及合并后的期望结果(combinedFilters1)。
我尝试了Object.assign()之类的方法或for循环(也包括在下面)中,但是我似乎无法获得我在下面概述的结果(combinedFilters1)。有什么建议么?非常感谢所有帮助。先感谢您。
// first object
this.filters1 = {
"categories":
[
{
"categoryName":"Video",
"categoryAttributes":
[
{
"name":"aspectRatio",
"values":["4:3", "16:15"]
},
{
"name":"Bit Rate",
"values":["256kbps", "512kbps"]
}
]
},
{
"categoryName":"Audio",
"categoryAttributes":
[
{
"name":"Speaker",
"values":["In-built", "External Connector"]
},
{
"name":"Bit Rate",
"values":["256kbps", "376kbps", "512kbps"]
}
]
}
]
};
// second object
this.filters2 = {
"categories":
[
{
"categoryName":"Video",
"categoryAttributes":
[
{
"name":"aspectRatio",
"values":["4:3", "16:15", "16:9"]
},
{
"name":"Bit Rate",
"values":["256kbps", "512kbps", "1024kbps"]
}
]
},
{
"categoryName":"Audio",
"categoryAttributes":
[
{
"name":"Speaker",
"values":["In-built", "External Connector"]
},
{
"name":"Bit Rate",
"values":["256kbps", "376kbps", "512kbps", "1024kbps"]
}
]
},
{
"categoryName":"OS",
"categoryAttributes":
[
{
"name":"Android",
"values":["Lolipop", "Marshmello"]
},
{
"name":"Apple",
"values":["IOS 5", "IOS 6"]
}
]
}
]
};
// desired outcome
this.combinedFilters1 = {
"categories":
[
{
"categoryName":"Video",
"categoryAttributes":
[
{
"name":"aspectRatio",
"values":["4:3", "16:15", "16:9"]
},
{
"name":"Bit Rate",
"values":["256kbps", "512kbps", "1024kbps"]
}
]
},
{
"categoryName":"Audio",
"categoryAttributes":
[
{
"name":"Speaker",
"values":["In-built", "External Connector"]
},
{
"name":"Bit Rate",
"values":["256kbps", "376kbps", "512kbps", "1024kbps"]
}
]
},
{
"categoryName":"OS",
"categoryAttributes":
[
{
"name":"Android",
"values":["Lolipop", "Marshmello"]
},
{
"name":"Apple",
"values":["IOS 5", "IOS 6"]
}
]
}
]
};
// attempted this loop
for (let i = 0, j = 0; i < this.filters1.categories.length,
j < this.filters2.categories.length; i++, j++) {
// console.log(this.filters1.categories[i].categoryName + " " + this.filters2.categories[j].categoryName);
if(this.filters1.categories.includes(this.filters2.categories[j].categoryName)) {
continue;
} else {
this.combinedFilters1.categories.push({"categoryName": this.filters2.categories[j].categoryName});
}
console.log(this.combinedFilters1);
for (let k = 0, l = 0; k < this.filters1.categories[i].categoryAttributes.length,
l < this.filters2.categories[j].categoryAttributes.length; k++, l++) {
console.log(this.filters1.categories[i].categoryAttributes[k]);
console.log(this.filters2.categories[j].categoryAttributes[l]);
if(this.filters1.categories.includes({"categoryAttributes" : [{"name": this.filters2.categories[j].categoryAttributes[l].name}]})) {
continue;
} else {
this.combinedFilters1.categories[i]["categoryAttributes"] = [{"name": this.filters2.categories[j].categoryAttributes[l].name}];
}
}
}
for循环的第一部分添加了唯一的类别(视频,音频,操作系统),但是for循环的第二部分没有添加唯一的属性,因此我需要对values数组执行相同的操作。非常感谢所有帮助,谢谢。
答案 0 :(得分:0)
如果您只想将两个数组合并为一个,则
this.combinedFilters1 = {categories: [...this.filters1.categories, ...this.filters2.categories]}
但是在“期望的结果”部分,您似乎正在覆盖filters1
的所有与categoryName
的元素相同的数组元素。如果那是您首先要尝试的操作,那么我将对数据进行结构化,以使类别是对象而不是数组,就像这样
filters2
然后做
this.filters1 = {
"categories": {
"Video": {
"categoryAttributes": [
{
"name":"aspectRatio",
"values":["4:3", "16:15"]
},
{
"name":"Bit Rate",
"values":["256kbps", "512kbps"]
}
]
},
"Audio": {/*data here*/}
}
}
这将使您的第二个行为覆盖第一个。
如果您确实无法更改数据结构,则必须进行一些效率低下的循环。
this.combinedFilters1 = {categories: {...this.filters1.categories, ...this.filters2.categories}}
注意:仅当this.combinedFilters1 = {...this.filters1};
for (const cat1 of this.combinedFilters1.categories) {
this.combinedFilters1.categories = this.filters2.categories.reduce((accumulator, cat2) => {
accumulator.push(cat1.categoryName === cat2.categoryName ? cat2 : cat1)
return accumulator;
}, [])
}
长于filters2
时,此代码才有效;
答案 1 :(得分:0)
您可以采用一个动态函数,该函数包含两个数组以及带有键和值名称的条目数组,一个处理最终级别和索引的函数。
function merge(a, b, entries, final, index = 0) {
var [k, v] = entries[index];
return b.reduce((r, o) => {
var temp = r.find(q => o[k] === q[k]);
if (!temp) {
r.push(o);
} else if (index + 1 === entries.length) {
temp[v] = final(temp[v], o[v]);
} else {
merge(temp[v], o[v], entries, final, index + 1);
}
return r;
}, a);
}
var filters1 = { categories: [{ categoryName: "Video", categoryAttributes: [{ name: "aspectRatio", values: ["4:3", "16:15"] }, { name: "Bit Rate", values: ["256kbps", "512kbps"] }] }, { categoryName: "Audio", categoryAttributes: [{ name: "Speaker", values: ["In-built", "External Connector"] }, { name: "Bit Rate", values: ["256kbps", "376kbps", "512kbps"] }] }] },
filters2 = { categories: [{ categoryName: "Video", categoryAttributes: [{ name: "aspectRatio", values: ["4:3", "16:15", "16:9"] }, { name: "Bit Rate", values: ["256kbps", "512kbps", "1024kbps"] }] }, { categoryName: "Audio", categoryAttributes: [{ name: "Speaker", values: ["In-built", "External Connector"] }, { name: "Bit Rate", values: ["256kbps", "376kbps", "512kbps", "1024kbps"] }] }, { categoryName: "OS", categoryAttributes: [{ name: "Android", values: ["Lolipop", "Marshmello"] }, { name: "Apple", values: ["IOS 5", "IOS 6"] }] }] },
parts = [
[undefined, 'categories'],
['categoryName', 'categoryAttributes'],
['name', 'values']
],
fn = (a, b) => [...new Set([...a, ...b])],
result = merge([filters1], [filters2], parts, fn);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }