0

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
        },
      ];
2
  • To make yours work as is just replace the references to Obj1 and Obj2 in your forEach() loops (ie: if (Obj1.dataKey === Obj2.dataKey && Obj2.fn) {... ) with item and newitem. And close your function calls properly Commented Jan 19, 2021 at 14:09
  • added a missing ) to your snippet so that it would run. Commented Jan 19, 2021 at 14:17

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.