Compare and add property to array of object

Tags: , ,



I have two arrays of objects (Obj1 and Obj2).

The difference is that Obj2 objects have an extra property called fn. I want to compare both arrays and if an object in Obj1 has fn in Obj2 for the same datakey then want to add fn in Obj1 also (the datakey is unique).

I don’t want to change the order of Obj1 array and I don’t want to remove any extra object from Obj1.

I tried the following but, it doesn’t seem to work or I am doing the wrong way.

let Obj1 = [
        {
          "dataKey": "aaa",
          "title": "Lorem ipsum",  
          "description": "Dolor sit",
          "flag": true
        },
        {
          "dataKey": "ccc",
          "title": "dsff fsfsfs",  
          "description": "dsd ds ds ds",
          "flag": false
        },
        {
          "dataKey": "bbb",
          "title": "Duis aute irure",  
          "description": "eu fugiat nulla pariatur",
          "flag": false
        },
        {
          "dataKey": "ddd",
          "title": "Lorem ipsum dsds",  
          "description": "Dolor sit dsdsds",
          "flag": true
        },
      ];

let Obj2 = [
        {
          "dataKey": "aaa",
          "title": "Lorem ipsum",  
          "description": "Dolor sit",
          "flag": true,
          "fn": function() {
            console.log('hi');
          }
        },
        {
          "dataKey": "bbb",
          "title": "Duis aute irure",  
          "description": "eu fugiat nulla pariatur",
          "flag": true
        },
        {
          "dataKey": "ccc",
          "title": "dsff fsfsfs",  
          "description": "dsd ds ds ds",
          "flag": true,
          "fn": function() {
            console.log('hi');
            return 'abcd';
          }
        },
      ];
      
  Obj1.forEach(function(item){
    Obj2.forEach(function(newitem) {
      if(item.dataKey === newitem.dataKey && newitem.fn) {
        item["fn"] = newitem.fn;
      }
    })
})

console.log(Obj1);

Expected Output:

let Obj1 = [
        {
          "dataKey": "aaa",
          "title": "Lorem ipsum",  
          "description": "Dolor sit",
          "flag": true,
          "fn": function() {
            console.log('hi');
          }
        },
        {
          "dataKey": "ccc",
          "title": "dsff fsfsfs",  
          "description": "dsd ds ds ds",
          "flag": false,
          "fn": function() {
            console.log('hi');
            return 'abcd';
          }
        },
        {
          "dataKey": "bbb",
          "title": "Duis aute irure",  
          "description": "eu fugiat nulla pariatur",
          "flag": false
        },
        {
          "dataKey": "ddd",
          "title": "Lorem ipsum dsds",  
          "description": "Dolor sit dsdsds",
          "flag": true
        },
      ];

Answer

Just iterate Obj2 and use Array.find() to find the corresponding value on Obj1

let Obj1 = [
        {
          "dataKey": "aaa",
          "title": "Lorem ipsum",  
          "description": "Dolor sit",
          "flag": true
        },
        {
          "dataKey": "ccc",
          "title": "dsff fsfsfs",  
          "description": "dsd ds ds ds",
          "flag": false
        },
        {
          "dataKey": "bbb",
          "title": "Duis aute irure",  
          "description": "eu fugiat nulla pariatur",
          "flag": false
        },
        {
          "dataKey": "ddd",
          "title": "Lorem ipsum dsds",  
          "description": "Dolor sit dsdsds",
          "flag": true
        },
      ];

let Obj2 = [
        {
          "dataKey": "aaa",
          "title": "Lorem ipsum",  
          "description": "Dolor sit",
          "flag": true,
          "fn": function() {
            console.log('hi');
          }
        },
        {
          "dataKey": "bbb",
          "title": "Duis aute irure",  
          "description": "eu fugiat nulla pariatur",
          "flag": true
        },
        {
          "dataKey": "ccc",
          "title": "dsff fsfsfs",  
          "description": "dsd ds ds ds",
          "flag": true,
          "fn": function() {
            console.log('hi');
            return 'abcd';
          }
        },
      ];
      
Obj2.forEach(function(newitem) {
  const obj = Obj1.find(item => item.dataKey === newitem.dataKey);
  if (newitem.fn)
    obj.fn = newitem.fn;
})

console.log(Obj1);


Source: stackoverflow