Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple javascript object properties at once

Tags:

javascript

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}
like image 294
David542 Avatar asked Oct 19 '25 16:10

David542


1 Answers

Object.assign can achieve it.

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

console.log(o);
like image 143
CertainPerformance Avatar answered Oct 21 '25 05:10

CertainPerformance