ketan
ketan

Reputation: 19341

Compare and add property to array of object

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
        },
      ];

Upvotes: 0

Views: 104

Answers (3)

vinod rawat
vinod rawat

Reputation: 39

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((val, key) => {
    Obj1.find((search) => {if(search.dataKey === val.dataKey && val.fn){
        search.fn = val.fn
    }});
    
})
console.log(Obj1)

Upvotes: 0

wangdev87
wangdev87

Reputation: 8751

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);

Upvotes: 1

Aykut Burak SAFAK
Aykut Burak SAFAK

Reputation: 332

I'd do something like below for this.

const obj1WithFn = Obj1.map(item => {
    const objFromOtherArray = Obj2.find(({dataKey}) => dataKey === item.dataKey);
    if(objFromOtherArray && objFromOtherArray.fn) {
        item.fn = objFromOtherArray.fn;
    }
    return item;
})

Upvotes: 0

Related Questions