2

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'
      }
   ]
}
1
  • Do you have a code snippet where you tried to achieve it ? Commented Mar 27, 2019 at 16:47

3 Answers 3

1

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)

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

2 Comments

It took me sometime to fully understand the logic and end up with doubt, what if I wanted to change the "e[key]" to update the value for another attribute in the currentObject, can it be done only by updating this line of code? e[key] = updateObject.myObject[value], thanks for the code this is good logic to improve my skills.
@bernlt yes you should change that line accordingly if you want to achieve anything else than you specified in question
1

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

2 Comments

Hey this is great, can you help me understand this line of code: finalObj = {[Object.keys(currentObject)[0]] : out };
@bernlt actually that is adding the myObject key in the finalObj output. so Object.keys will return the array of keys in the currentObject which will be [myObject] but I want the string value so I need to take the '0'th index from the array.
1

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.

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.