From a list of IDs with empty and non-empty values, retrieve specific ID records in JavaScript


Let’s say the following is our list −

var details=[
   {id:101,name:"John",age:21},
   {id:111,name:"David",age:24},
   {id:1,name:"Mike",age:22},
   {id:"",name:"Sam",age:20},
   {id: 1,name:"Carol",age:23},
   {id:null,name:"Robert",age:25},
   {id:1,name:"Adam",age:24},
   {id:"",name:"Chris",age:23}
];

You can use the concept of filter to retrieve values based on specific ID.

Example

var details=[
   {id:101,name:"John",age:21},
   {id:111,name:"David",age:24},
   {id:1,name:"Mike",age:22},
   {id:"",name:"Sam",age:20},
   {id: 1,name:"Carol",age:23},
   {id:null,name:"Robert",age:25},
   {id:1,name:"Adam",age:24},
   {id:"",name:"Chris",age:23}
];
var getIdWithValue1 = details.filter(obj => obj.id === 1);
console.log(getIdWithValue1);

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

node fileName.js.

Here, my file name is demo181.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo181.js
[
   { id: 1, name: 'Mike', age: 22 },
   { id: 1, name: 'Carol', age: 23 },
   { id: 1, name: 'Adam', age: 24 }
]

Updated on: 12-Sep-2020

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements