gpt4 book ai didi

javascript - 如何合并不同属性和长度的对象而不重复

转载 作者:行者123 更新时间:2023-11-28 03:40:35 25 4
gpt4 key购买 nike

我正在尝试合并两个具有相同键/关联数组和不同键的对象。关联的数组也具有不同的长度。为了让您有更好的了解,我在下面添加了一些代码,这些代码显示了我尝试合并的两个对象(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 循环的第二部分不添加独特的属性,我需要对值数组执行相同的操作。非常感谢所有帮助,谢谢。

最佳答案

您可以采用一个动态函数,该函数采用两个数组、带有键和值名称的条目数组、一个处理最终级别的函数和一个索引。

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; }

关于javascript - 如何合并不同属性和长度的对象而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57331675/

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