从一个带有空值和非空值的 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} ];
你可以使用 filter 的概念根据特定的 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 } ]
广告