I am attempting to merge two objects that both, have some same keys/associated arrays and different keys. The associated arrays are also of different length. To give you a better picture, I have included some code below that shows the two objects I am trying to merge (filters1 and filters2) and the desired outcome after merging (combinedFilters1).
I've tried methods like Object.assign() or for loops (which I have also included below), but I can't seem to get the result I outlined below (combinedFilters1). Any suggestions? All help is greatly appreciated. Thank you in advance.
// 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}];
}
}
}
The first part of the for loop adds the unique categories (video, audio, OS), but the second part of the for loop doesn't add the unique attributes and I need to do the same for the values array. All help is greatly appreciated, thank you.