Update multiple values of object in JavaScript [duplicate]

  • A+
Category:Languages

This question already has an answer here:

Given an object

var myObject = {     label: 'foo',     name: 'bar',     id: 12 }, 

If I wanted to change multiple values, I would do the following:

myObject.label = "bar"; myObject.name = "foo"; 

When updating large sets of data, it makes the code quite blocky. Is there a way to do this in a more concise manner?

Like:

myObject.({label: 'foo'}, {name: 'bar'}); 

 


Object.assign is nice for this:

var myObject = {     label: 'foo',     name: 'bar',     id: 12 } Object.assign(myObject, {label: 'Test', name: 'Barbar'}) console.log(myObject)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: