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

JavascriptWeb DevelopmentObject Oriented Programming

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' } }
]
raja
Published on 03-Sep-2020 09:29:52
Advertisements