'How to assign an array of objects to an object?

I have the following object and array of objects the later is a server array and the first one is a reference object that I want to use to get the values that I want from the main array and idea how to intigrate the array in to the object ? I tried to use Object.assign but no luck.

const obj1 = { day1: ['cake', 'muffin'], day2: ['grapes']};
const arrObj2: = [{name : 'cake', group 'dessert'},{name : 'muffin', group 'dessert'},{name : 'grapes', group 'vegs'}]
// Expected results 
const newObject = //{ 
                  //  day1: [{name : 'cake', group 'dessert'},{name : 'muffin', group 'dessert'}]
                  //, day2: [{name : 'grapes', group 'vegs'}]
                  //};


Solution 1:[1]

You can use .filter():

const obj1 = { day1: ['cake', 'muffin'], day2: ['grapes']};
const arrObj2 = [{name : 'cake', group: 'dessert'},{name : 'muffin', group: 'dessert'},{name : 'grapes', group: 'vegs'}];
// Expected results 
const newObj = {
  day1: arrObj2.filter((el) => obj1.day1.includes(el.name)),
  day2: arrObj2.filter((el) => obj1.day2.includes(el.name))
};

console.log(newObj);

Solution 2:[2]

Your code is not a valid Javascript. There should not be a colon after the variable name. I recommend you to learn about variables and loops in programming.

But to your answer, just loop through data and replace values:

for(var day in obj1) 
    for(var i=0; i<obj1[day].length; i++) 
        for(var j=0; j<arrObj2.length; j++) 
            if(obj1[day][i]==arrObj2[j].name) obj1[day][i]=arrObj2[j];

I think you should represent "arrObj2" as an object (string-to-string map).

var GroupMap = {"cake":"dessert","muffin":"desert","grapes":"vegs"};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 2pichar
Solution 2 Ivan Kuckir