Categories
JavaScript Answers

How to remove value from object without mutation with JavaScript?

Spread the love

Sometimes, we want to remove value from object without mutation with JavaScript.

In this article, we’ll look at how to remove value from object without mutation with JavaScript.

How to remove value from object without mutation with JavaScript?

To remove value from object without mutation with JavaScript, we can use the rest syntax.

For instance, we write

const omitProp = (obj, prop) => {
  const { [prop]: omit, ...res } = obj;
  return res;
};

to create the omitProp function that destructures the prop property from the obj object.

And then we get the rest of the properties in obj with the res object.

Finally, we return res.

Conclusion

To remove value from object without mutation with JavaScript, we can use the rest syntax.

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 *