I am looking to find a way to compare and combine two multidimensinal array of objects based on course values. I can make it work using the filter method for the top level, but not sure how to make sure that all levelwithskills are deeply merged. Here is an example:
var array1 = [
{
"course": "Javascript and protractor",
"description": "this is test course",
"levelwithskills": [
{
"name": "Beginner",
"skills": [
{
"duration": 45,
"name": "HTML/CSS/JS",
"price": 75
},
{
"duration": 45,
"name": "Data And Flows",
"price": 75
},
{
"duration": 45,
"name": "Functions And Objects",
"price": 75
},
{
"duration": 45,
"name": "Protractor Project",
"price": 75
}
]
},
{
"name": "Improver",
"skills": [
{
"duration": 45,
"name": "Jasmine",
"price": 90
},
{
"duration": 45,
"name": "Protractor Methods",
"price": 90
},
{
"duration": 45,
"name": "Non Angular App Testing",
"price": 90
},
{
"duration": 45,
"name": "Reports",
"price": 90
}
]
},
{
"name": "Intermediate",
"skills": [
{
"duration": 45,
"name": "Crossbrowser Config",
"price": 100
},
{
"duration": 45,
"name": "Data-Driven",
"price": 100
},
{
"duration": 45,
"name": "Gulp-Integration",
"price": 100
},
{
"duration": 45,
"name": "User Stories To Test",
"price": 100
}
]
}
],
"s3key": "javascsipt.png"
},
{
"course": "Java and BDD",
"id": "564e0758-803f-11e9-ba30-01cdbe15a808",
"levelwithskills": [
{
"name": "Beginner",
"skills": [
{
"duration": 45,
"name": "HTML/CSS/JS",
"price": 1
},
{
"duration": 45,
"name": "Data and flows",
"price": 1
},
{
"duration": 45,
"name": "OOP",
"price": 1
},
{
"duration": 45,
"name": "Libraries",
"price": 1
}
]
},
{
"name": "Improver",
"skills": [
{
"duration": 45,
"name": "Selenium Webdriver",
"price": 90
},
{
"duration": 45,
"name": "Junit",
"price": 90
},
{
"duration": 45,
"name": "Saucelabs",
"price": 90
},
{
"duration": 45,
"name": "User Stories To Test",
"price": 90
}
]
},
{
"name": "Intermediate",
"skills": [
{
"duration": 45,
"name": "Most Used Methods",
"price": 100
},
{
"duration": 45,
"name": "Git",
"price": 100
},
{
"duration": 45,
"name": "Gherkin",
"price": 100
},
{
"duration": 45,
"name": "Cucumber",
"price": 100
}
]
},
{
"name": "Semi-Advance",
"skills": [
{
"duration": 45,
"name": "Maven",
"price": 110
},
{
"duration": 45,
"name": "Cucumber Data-driven",
"price": 11000
},
{
"duration": 45,
"name": "Jenkins",
"price": 110
},
{
"duration": 45,
"name": "Pipeline",
"price": 110
}
]
},
{
"name": "Advance",
"skills": [
{
"duration": 45,
"name": "Page Object Model",
"price": 120
},
{
"duration": 45,
"name": "Advanced Methods",
"price": 120
},
{
"duration": 45,
"name": "Advanced Logic",
"price": 120
},
{
"duration": 45,
"name": "Your Own Framework",
"price": 120
}
]
},
{
"name": "Professional",
"skills": [
{
"duration": 45,
"name": "Serenit Framework p1",
"price": 130
},
{
"duration": 45,
"name": "Serenit Framework p2",
"price": 130
},
{
"duration": 45,
"name": "Serenit Framework p3",
"price": 130
},
{
"duration": 45,
"name": "Serenit Framework p4",
"price": 130
}
]
}
],
"passion": "Tester",
"s3key": "java.png",
}
];
var array2 = [
{
"course": "Javascript and protractor",
"levelwithskills": [
{
"name": "Beginner",
"skills": [
{
"name": "HTML/CSS/JS"
}
]
}
]
}
,
{
"course": "Java and BDD",
"levelwithskills": [
{
"name": "Beginner",
"skills": [
{
"name": "HTML/CSS/JS"
}
]
}
]
}
];
const sameData = array1.map(
obj =>
array2.map(obj2 => {
if (obj.course === obj2.course) {
return {
...obj,
levelwithskills: obj.levelwithskills.map(
xlvl =>
obj2.levelwithskills.map(ylvl => {
if (xlvl.name === ylvl.name) {
return {
...xlvl,
skills: xlvl.skills.map(
xskill =>
ylvl.skills.map(yskill => {
if (xskill.name === yskill.name) {
return {
...xskill,
selected: true
};
}
return xskill;
})[0]
)
};
}
return xlvl;
})[0]
)
};
}
})[0]
);
console.log(sameData);
I am looking to take something like that and merge it like:
[
{
"course": "Javascript and protractor",
"description": "this is test course",
"levelwithskills": [
{
"name": "Beginner",
"skills": [
{
"duration": 45,
"name": "HTML/CSS/JS",
"price": 75,
"selected": true
},
{
"duration": 45,
"name": "Data And Flows",
"price": 75
},
{
"duration": 45,
"name": "Functions And Objects",
"price": 75
},
{
"duration": 45,
"name": "Protractor Project",
"price": 75
}
]
},
{
"name": "Improver",
"skills": [
{
"duration": 45,
"name": "Jasmine",
"price": 90
},
{
"duration": 45,
"name": "Protractor Methods",
"price": 90
},
{
"duration": 45,
"name": "Non Angular App Testing",
"price": 90
},
{
"duration": 45,
"name": "Reports",
"price": 90
}
]
},
{
"name": "Intermediate",
"skills": [
{
"duration": 45,
"name": "Crossbrowser Config",
"price": 100
},
{
"duration": 45,
"name": "Data-Driven",
"price": 100
},
{
"duration": 45,
"name": "Gulp-Integration",
"price": 100
},
{
"duration": 45,
"name": "User Stories To Test",
"price": 100
}
]
}
],
"s3key": "javascsipt.png"
},
{
"course": "Java and BDD",
"id": "564e0758-803f-11e9-ba30-01cdbe15a808",
"levelwithskills": [
{
"name": "Beginner",
"skills": [
{
"duration": 45,
"name": "HTML/CSS/JS",
"price": 1
"selected": true
},
{
"duration": 45,
"name": "Data and flows",
"price": 1
},
{
"duration": 45,
"name": "OOP",
"price": 1
},
{
"duration": 45,
"name": "Libraries",
"price": 1
}
]
},
{
"name": "Improver",
"skills": [
{
"duration": 45,
"name": "Selenium Webdriver",
"price": 90
},
{
"duration": 45,
"name": "Junit",
"price": 90
},
{
"duration": 45,
"name": "Saucelabs",
"price": 90
},
{
"duration": 45,
"name": "User Stories To Test",
"price": 90
}
]
},
{
"name": "Intermediate",
"skills": [
{
"duration": 45,
"name": "Most Used Methods",
"price": 100
},
{
"duration": 45,
"name": "Git",
"price": 100
},
{
"duration": 45,
"name": "Gherkin",
"price": 100
},
{
"duration": 45,
"name": "Cucumber",
"price": 100
}
]
},
{
"name": "Semi-Advance",
"skills": [
{
"duration": 45,
"name": "Maven",
"price": 110
},
{
"duration": 45,
"name": "Cucumber Data-driven",
"price": 11000
},
{
"duration": 45,
"name": "Jenkins",
"price": 110
},
{
"duration": 45,
"name": "Pipeline",
"price": 110
}
]
},
{
"name": "Advance",
"skills": [
{
"duration": 45,
"name": "Page Object Model",
"price": 120
},
{
"duration": 45,
"name": "Advanced Methods",
"price": 120
},
{
"duration": 45,
"name": "Advanced Logic",
"price": 120
},
{
"duration": 45,
"name": "Your Own Framework",
"price": 120
}
]
},
{
"name": "Professional",
"skills": [
{
"duration": 45,
"name": "Serenit Framework p1",
"price": 130
},
{
"duration": 45,
"name": "Serenit Framework p2",
"price": 130
},
{
"duration": 45,
"name": "Serenit Framework p3",
"price": 130
},
{
"duration": 45,
"name": "Serenit Framework p4",
"price": 130
}
]
}
],
"passion": "Tester",
"s3key": "java.png",
}
]