- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 } ]
Advertisements