Sometimes, we want to remove a property in an object immutably with JavaScript.
In this article, we’ll look at how to remove a property in an object immutably with JavaScript.
How to remove a property in an object immutably with JavaScript?
To remove a property in an object immutably with JavaScript, we can use destructuring.
For instance, we write
const state = {
c: {
x: "42",
y: "43",
},
};
const {
c: { y, ...c },
} = state;
console.log({ ...state, c });
to get various properties from the state
object with
const {
c: { y, ...c },
} = state;
And then we spread the properties of state
into a new object and put the value of c
as a property of the new object with
{ ...state, c }
Conclusion
To remove a property in an object immutably with JavaScript, we can use destructuring.