bernlt
bernlt

Reputation: 445

JavaScript update Object value from another object

I have two objects, one is used to update the other, something like ETL Process.

const currentObject = {
    myObject : [
      {
        'attribute1':'foo1',
        'attribute2':'bar1',
        'attribute3':'test1'
      },
      {
        'attribute1':'foo2',
        'attribute2':'bar2',
        'attribute3':'test2'
      },
      {
       'attribute1':'foo3',
       'attribute2':'bar3',
       'attribute3':'test3'
      },
   ]
}

if the attribute3 value is "test1", then go to the other object and check for the test1 property and replace the currentObject with the new value

const updateObject = {
  myObject : {
    'test1':'newtest1',
    'test2':'newtest2',
    'test3':'newtest3'
  }
}

update is done on currentObject attribute3 needs to use updateObject property as reference; where currentObject attribute1="test1" should copy data from updateObject test1 so on:

Final value should be like:

const currentObject = {
    myObject : [
      {
        'attribute1':'foo1',
        'attribute2':'bar1',
        'attribute3':'newtest1'
      },
      {
        'attribute1':'foo2',
        'attribute2':'bar2',
        'attribute3':'newtest2'
      },
      {
       'attribute1':'foo3',
       'attribute2':'bar3',
       'attribute3':'newtest3'
      }
   ]
}

Upvotes: 2

Views: 3018

Answers (3)

Brian Adams
Brian Adams

Reputation: 45810

This turns into a one-liner with the latest JavaScript language features:

const currentObject = {
  myObject: [
    {
      'attribute1': 'foo1',
      'attribute2': 'bar1',
      'attribute3': 'test1'
    },
    {
      'attribute1': 'foo2',
      'attribute2': 'bar2',
      'attribute3': 'test2'
    },
    {
      'attribute1': 'foo3',
      'attribute2': 'bar3',
      'attribute3': 'test3'
    },
  ]
}

const updateObject = {
  myObject: {
    'test1': 'newtest1',
    'test2': 'newtest2',
    'test3': 'newtest3'
  }
}

const result = { myObject: currentObject.myObject.map(o => ({ ...o, ...{ 'attribute3': updateObject.myObject[o.attribute3] } })) };

console.log(result);

...and you also get immutability.

Upvotes: 1

Amardeep Bhowmick
Amardeep Bhowmick

Reputation: 16908

We can use Array.reduce and search for the current element's (ele) attribute3 property in the updateObject.myObject.

If present then update it with the matching value from the updateObject.myObject else keep the old one :

const currentObject = {myObject : [{'attribute1':'foo1','attribute2':'bar1','attribute3':'test1'},{'attribute1':'foo2','attribute2':'bar2','attribute3':'test2'},{'attribute1':'foo3','attribute2':'bar3','attribute3':'test3'},]};
const updateObject = {myObject : {'test1':'newtest1','test2':'newtest2','test3':'newtest3'}};

function transformObject(currentObject, updateObject){
    const out = currentObject.myObject.reduce((acc, ele) => {
       ele.attribute3 = updateObject.myObject[ele.attribute3] ?
                        updateObject.myObject[ele.attribute3] : 
                        ele.attribute3;
       return acc.concat(ele);
    }, []);
    finalObj = {[Object.keys(currentObject)[0]] : out };
    return finalObj;
}
console.log(transformObject(currentObject, updateObject));

Upvotes: 1

Code Maniac
Code Maniac

Reputation: 37755

You can use forEach and Object.entries

Here idea is

  • First loop through each element in myObject array of currentObject
  • Now as in your structure you have value of currentObject as key in updateObject, so we check existence by updateObject.myObject[value]
  • If it's their we update currentObject else we keep it unchanged

const currentObject = {myObject : [{'attribute1':'foo1','attribute2':'bar1','attribute3':'test1'},{'attribute1':'foo2','attribute2':'bar2','attribute3':'test2'},{'attribute1':'foo3','attribute2':'bar3','attribute3':'test3'},]}
const updateObject = {myObject : {'test1':'newtest1','test2':'newtest2','test3':'newtest3'}}

currentObject.myObject.forEach(e => {

Object.entries(e).forEach(([key,value]) => {
    if(updateObject.myObject[value]){
      e[key] = updateObject.myObject[value]
    }
  })
})

console.log(currentObject)

Upvotes: 1

Related Questions