One possible solution is to loop through each object in Object1 and check if its "guideLink" property matches a key in Object2. If there is a match, then extract the "filters" property from the corresponding Object2 object and add it to the Object1 object as a new "filters" property. Here's an example implementation in JavaScript:
for (let obj1 of Object.values(majors)) {
const guideLink = obj1.guideLink;
if (majorsWithFilters.hasOwnProperty(guideLink)) {
const filtersStr = majorsWithFilters[guideLink];
const filtersArr = filtersStr.split(' ').map(str => parseInt(str.replace('filter_', '')));
obj1.filters = filtersArr;
}
}
Explanation:
- We use a
for...ofloop to iterate through each object in Object1 (majors). - For each object, we extract its "guideLink" property and check if it exists as a key in Object2 (
majorsWithFilters) using thehasOwnPropertymethod. - If there is a match, we extract the "filters" property from the corresponding Object2 object (which is a string of space-separated filter numbers), split it into an array of integers using
splitandmap, and assign it to a new "filters" property in the Object1 object. - Note that we use
parseIntandreplaceto convert the filter strings from "filter_XX" format to integers.
This should result in the desired merged objects with "filters" properties.