I've been trying to figure out how to do recursion for 2 objects that have similar properties, but also have differences. I need to merge these 2 objects in a unique way, so there are no duplicate countries or models etc.
EDIT: in vanilla js only please
var us1 = {
country: {
"United States": {
"Ford": {
"engine": {
type1: "4 cyl",
type2: "6 cyl"
}
},
"Chevy": {
"engine": {
type1: "6 cyl"
}
}
}
}
}
var us2 = {
country: {
"United States": {
"Ford": {
"engine": {
type3: "12 cyl"
}
},
"Saturn": {
"engine": {
type1: "4 cyl"
}
}
}
}
}
var cars = [us1, us2];
var newCars = [];
function fn(cars) {
if (typeof cars == "object") {
for (var attr in cars) {
if (!newCars.hasOwnProperty(cars[attr])) {
newCars.push(cars[attr]);
}
fn(cars[attr])
}
} else {
//
}
}
console.log(fn(cars));
console.log(newCars)
Result wanted:
var us1 = {
country: {
"United States": {
"Ford": {
"engine": {
type1: "4 cyl",
type2: "6 cyl",
type2: "12 cyl"
}
},
"Chevy": {
"engine": {
type1: "6 cyl"
}
},
"Saturn": {
"engine": {
type1: "4 cyl"
}
}
}
}
}