How to delete an index from a JSON Object ?


While working with JSON objects, Sometimes we need to delete an index or key from the JSON object to remove unwanted data or modify the structure. We can delete the index from a JSON object using methods like the delete operator, filter method, and lodash library. In this article, we will explore these different methods to delete an index from a JSON object.

Prerequisites

To follow along with the examples provided in this article, you should have a basic understanding of JavaScript and JSON syntax. Additionally, you will need a text editor or an integrated development environment (IDE) to write and execute the code snippets.

Method 1:Using the delete Operator

The delete operator is a built−in JavaScript operator that allows us to remove a property from an object, including JSON objects.

Syntax

delete objectName.propertyName

Here, objectName represents the name of the object from which you want to delete the property, and propertyName is the name of the property you wish to remove.

Example

In the below example, we have a JSON object named jsonObject. The delete operator is used to remove the age property from the object. The output shows that the age property has been successfully deleted from the JSON object.

let jsonObject = {
  "name": "John Doe",
  "age": 30,
  "email": "johndoe@example.com"
};

delete jsonObject.age;

console.log(jsonObject);

Output

{
  "name": "John Doe",
  "email": "johndoe@example.com"
}

Method 2:Using the filter Method

The filter method is a powerful array method in JavaScript that allows us to create a new array by filtering out elements based on a given condition. By combining the filter method with Object.entries and Object.fromEntries, we can remove a specific index from a JSON object.

Syntax

let newArray = arrayName.filter((element, index, array) => {
  // return a boolean value based on the condition
});

Here, arrayName refers to the name of the array from which you want to create a new filtered array. The filter method takes a callback function as an argument, which is executed for each element in the array. Inside the callback function, you define the condition based on which elements are filtered. The callback function can receive three parameters: element (the current element being processed), index (the index of the current element), and array (the original array). The filter method returns a new array that contains the elements for which the callback function returns true.

Example

In the below example, we first convert the JSON object jsonObject into an array of key−value pairs using Object.entries. Then, we use the filter method to create a new array, excluding the index with the key "age". Finally, we convert the filtered array back into a JSON object using Object.fromEntries. The resulting object, filteredObject, is printed to the console.

let jsonObject = {
  "name": "John Doe",
  "age": 30,
  "email": "johndoe@example.com"
};

let filteredObject = Object.fromEntries(
  Object.entries(jsonObject).filter(([key, value]) => key !== "age")
);

console.log(filteredObject);

Output

{ name: 'John Doe', email: 'johndoe@example.com' }

Method 3:Using the lodash Library

The lodash library is a popular JavaScript utility library that is used in a wide range of functions for manipulating data, including JSON objects. The omit function from the lodash library can be used to remove a specific index from a JSON object.

Syntax

let filteredObject = _.omit(objectName, property1, property2, ...);

Here, _.omit() is a function provided by the lodash library, which allows you to remove specific properties from an object. The objectName represents the name of the object from which you want to remove properties. You can specify the properties you want to remove as additional arguments to the _.omit() function, separated by commas. The _.omit() method returns a new object that contains all the properties of the original object except the ones specified.

Example

In the below example, we import the lodash library using the require statement. The _.omit function is then used to remove the index with the key "age" from the JSON object jsonObject. The resulting object, filteredObject, is displayed in the console.

const _ = require('lodash');

let jsonObject = {
  "name": "John Doe",
  "age": 30,
  "email": "johndoe@example.com"
};

let filteredObject = _.omit(jsonObject, "age");

console.log(filteredObject);

Output

{
  "name": "John Doe",
  "email": "johndoe@example.com"
}

Conclusion

In this article, we discussed how we can delete an index from a JSON object using different methods and libraries in javascript. We discussed the usage of the delete operator, the filter method combined with Object.entries and Object.fromEntries, as well as the omit function from the lodash library. Each method provides a distinct way to remove unwanted data from a JSON object, allowing you to implement/design the object structure to your specific needs.

Updated on: 17-Jul-2023

964 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements