1

Lets say I have an object

obj = {
    name: 'Karthi',
    age: 25,
    gender: 'Male'
}

and I want to assign some of the values to a key with the same name in another object.

objTar = {
    name: obj.name,
    age: obj.age
}

Is there a shorthand for the above "nested" assignment? I know there's a shorthand for flat assignments; eg., objTar = { name, age } given name and age are available in the lexical scope.

Don't suggest workarounds to achieve this please. Just let me know if this is "natively" supported.

karthikaruna
  • 3,042
  • 7
  • 26
  • 37

1 Answers1

-1

You can achieve this using below code.

objTar = Object.assign({},{age:obj.age, gender:obj.gender})
Dheeraj Kumar
  • 146
  • 1
  • 10