Categories
JavaScript Answers

How to remove object properties with Lodash and JavaScript?

Spread the love

Sometimes, we want to remove object properties with Lodash.

In this article, we’ll look at how to remove object properties with Lodash.

How to remove object properties with Lodash?

To remove object properties with Lodash, we can use the pick function.

For instance, we write

const model = {
  fName: null,
  lName: null,
};

const credentials = {
  fName: "xyz",
  lName: "abc",
  age: 23,
};

const result = _.pick(credentials, _.keys(model));

console.log(result);

to get the array of property keys from the model object with the keys function.

Then we call pick with credentials and the array of keys returned by the keys function to return a new object with the properties with the property names that are in the model object.

Conclusion

To remove object properties with Lodash, we can use the pick function.

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 *