Remove json element - JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Let’s say the following is our JSON string −

var details =
[
   {
      customerName: "Chris",
      customerAge: 32
   },
   {
      customerName: "David",
      customerAge: 26
   },
   {
      customerName: "Bob",
      customerAge: 29
   },
   {
      customerName: "Carol",
      customerAge: 25
   }
]

To remove JSON element, use the delete keyword in JavaScript.

Example

Following is the complete code to remove JSON element −

var details =
[
   {
      customerName: "Chris",
      customerAge: 32
   },
   {
      customerName: "David",
      customerAge: 26
   },
   {
      customerName: "Bob",
      customerAge: 29
   },
   {
      customerName: "Carol",
      customerAge: 25
   }
]
delete details[0].customerAge;
console.log(details);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo211.js −

PS C:\Users\Amit\JavaScript-code> node demo211.js
[
   { customerName: 'Chris' },
   { customerName: 'David', customerAge: 26 },
   { customerName: 'Bob', customerAge: 29 },
   { customerName: 'Carol', customerAge: 25 }
]
raja
Updated on 01-Oct-2020 13:36:31

Advertisements