0

I have two objects:

objectA = {
  valueOne: null,
  valueTwo: null
}
objectB = {
  valueZero: 'joe',
  valueOne 'john',
  valueTwo: 'jesse',
  valueThree: 'jessica',
  valueFour 'jojo'
}

I want two assign objectB property values to objectA properties but only those who have the same name. For example in our case objectA should become like this:

objectA = {
  valueOne: 'john',
  valueTwo: 'jesse'
}

How can I do it?

seyet
  • 1,080
  • 4
  • 23
  • 42
  • Please may you add your own attempt as a [mcve]? – evolutionxbox Nov 28 '21 at 01:10
  • I was adding an answer, since the duplicate doesn't quite work... `Object.fromEntries(Object.keys(objectA).map(key => [key, objectB[key]]))` --- This takes the keys of A, maps them to an array of key/value pairs, and then converts them back into an object. – evolutionxbox Nov 28 '21 at 01:13
  • Object.keys(objectB).forEach(key => { if (key in objectA) objectA[key] = objectB[key] }); – deafening_roar Nov 28 '21 at 01:18

0 Answers0