To create a new dictionary in JavaScript from another dictionary, you can use the spread operator or Object.assign() method.
Here's an example using the spread operator:
const originalDict = { name: "John", age: 30, occupation: "developer" };
const newDict = { ...originalDict };
console.log(newDict); // { name: "John", age: 30, occupation: "developer" }
Here's an example using Object.assign():
const originalDict = { name: "John", age: 30, occupation: "developer" };
const newDict = Object.assign({}, originalDict);
console.log(newDict); // { name: "John", age: 30, occupation: "developer" }
Both methods create a new object that has the same key-value pairs as the original dictionary. If you modify the original dictionary after creating a new one, the new dictionary will not be affected.
Asked: 2022-10-17 11:00:00 +0000
Seen: 18 times
Last updated: May 04 '21