lecongly
In JavaScript, you can use the three dots (…) operator to spread the properties of an object to where one or more key-value pair is expected.
For example:
const data = { name: "Alice", age: 26 }
const dataCopy = { ...data }
data.age = 1000
console.log(data)
console.log(dataCopy)
Output:
{
age: 1000,
name: "Alice"
}
{
age: 26,
name: "Alice"
}
const data = { name: "Alice", age: 26 }
const dataCopy = Object.assign({}, data)
data.age = 1000
console.log(data)
console.log(dataCopy)
Output:
{
age: 1000,
name: "Alice"
}
{
age: 26,
name: "Alice"
}
const data = { name: "Alice", age: 26 }
const dataCopy = JSON.parse(JSON.stringify(data))
data.age = 1000
console.log(data)
console.log(dataCopy)
Output:
{
age: 1000,
name: "Alice"
}
{
age: 26,
name: "Alice"
}
const data = { name: "Alice", age: 26 }
const dataCopy = structuredClone(data)
data.age = 1000
console.log(data)
console.log(dataCopy)
Output:
{
age: 1000,
name: "Alice"
}
{
age: 26,
name: "Alice"
}