How to remove an object using filter() in JavaScript?


Let’s say, we have the following objects in JavaScript −

ObjectValue =[
   { "id": "101", "details": { Name:"John",subjectName:"JavaScript" }},
   { "id": "102", "details": { Name:"David",subjectName:"MongoDB" }},
   { "id": "103" }
]

To remove an object using filter(), the code is as follows −

Example

ObjectValue =[
   { "id": "101", "details": { Name:"John",subjectName:"JavaScript" }},
   { "id": "102", "details": { Name:"David",subjectName:"MongoDB" }},
   { "id": "103" }
]
output=ObjectValue.filter(obj=>obj.details)
console.log(output)

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

node fileName.js.

Here, my file name is demo46.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo46.js
[
   { id: '101', details: { Name: 'John', subjectName: 'JavaScript' } },
   { id: '102', details: { Name: 'David', subjectName: 'MongoDB' } }
]

Updated on: 03-Sep-2020

504 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements