9951 explained code solutions for 126 technologies


javascript-lodashHow can I delete a key from an object using Lodash in JavaScript?


Using Lodash in JavaScript, you can delete a key from an object by using the _.omit() method. This method takes two arguments, the object and the key to delete.

Example code

const _ = require('lodash');

const obj = {
  name: 'John Doe',
  age: 25
};

const updatedObj = _.omit(obj, 'age');

console.log(updatedObj);

Output example

{ name: 'John Doe' }

The code above does the following:

  1. Require the Lodash library with const _ = require('lodash');
  2. Create an object with two keys name and age
  3. Use the _.omit() method to delete the age key from the object
  4. Log the updated object to the console

Relevant link: Lodash Documentation

Edit this code on GitHub