Home > Net >  Update multiple javascript object properties at once
Update multiple javascript object properties at once

Time:10-09

Is there a short-hand to do the following:

let o = {}
o['a'] = 1;
o['b'] = 2;

For example, either:

let o = {}
o.update({'a': 1, 'b': 2})

Or:

let o = {'a': 1, 'b': 2}

CodePudding user response:

Object.assign can achieve it.

const o = { original: 'orig' };
Object.assign(o, {'a': 1, 'b': 2});

console.log(o);

  • Related