python dict.update() equivalent in javascript

  • If you are using a Map instance, just call yourMap.set(key, newValue).
  • If you are using a basic JavaScript object, just use either:
    • Dot-notation yourObject.yourProperty = newValue
    • Square brackets yourObject['your-property'] = newValue

There's no "bulk" update like Python. That will depend on how you structure your data and how deep down you need to go.


You can use Object.assign() to get the job done. It will create a new object with all the keys updated as well as add any new key value pairs to the newly created object.

const target = {c: 4, d: 5}
const source = {a: 1, b: 2, c: 3};

const newObj = Object.assign({}, target, source);

console.log(newObj); //=> {a: 1, b: 2, c: 3, d: 5}

Note that Object.assign() modifies the first argument which is why we're passing an empty object above.

You can even pass in a sequence of objects to update the target object, check out the MDN link for more info


You can use the destructuring assignment.

const first_dict = { 'key_one': 'value_one', 'key_two': 'value_two' } 
const second_dict = { 'key_one': 'value_from_second_dict', 'key_three': 'value_three' }

const accumulative = {
  ...first_dict,
  ...second_dict
}

console.log(accumulative)

/* 
[object Object] {
  key_one: "value_from_second_dict",
  key_three: "value_three",
  key_two: "value_two"
}
*/