Categories
JavaScript Answers

What is the equivalent of Python’s dict.update() in JavaScript?

Spread the love

Sometimes, we want to update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript.

In this article, we’ll look at how to update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript.

What is the equivalent of Python’s dict.update() in JavaScript?

To update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript, we can use the spread operator.

For instance, we write:

const marks = {
  'Physics': 67,
  'Maths': 87
}
const internalMarks = {
  'Practical': 48
}
const newMarks = {
  ...marks,
  ...internalMarks
}
console.log(newMarks)

to merge the marks and internalMarks object into the newMarks object with the spread operator.

As a result, newMarks is {Physics: 67, Maths: 87, Practical: 48}.

Conclusion

To update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript, we can use the spread operator.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *