从具有空值和非空值的ID列表中,检索JavaScript中的特定ID记录

假设以下是我们的列表-

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}
];

您可以使用过滤器的概念根据特定ID检索值。

示例

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);

要运行上述程序,您需要使用以下命令-

node fileName.js.

在这里,我的文件名为demo181.js。

输出结果

这将产生以下输出-

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 }
]