我有一个对象,我想过滤它的键..
我试图通过ID过滤对象,如下所示:
let myKeys = Object.keys(data).filter(function(key) {
//console.log(data[key]);
if(parseInt(key) === parseInt(vm.system_id)) {
return data[key];
}
});
console.log(myKeys);
这部分工作 - 我正在获取密钥,但是,我没有得到此项目下的所有数据/项目即时过滤
对象即时过滤与此类似:
{
"646": [{
"id": 52144,
"timestamp": "2017-08-17T14:10:23Z",
"type": "alarm",
"code": 210,
"title": "",
"description": "",
"remedy": "",
"appeared": "2017-08-17T14:10:09Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "CG-MX19D7K5C1",
"system_id": 646,
"system_device_id": 458,
"stream": "cu351.alarm_code"
}
],
"693": [{
"id": 51675,
"timestamp": "2017-08-16T13:59:55Z",
"type": "alarm",
"code": 215,
"title": "",
"description": "",
"remedy": "",
"appeared": "2017-08-16T13:59:57Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Demo 07122016",
"system_id": 693,
"system_device_id": 371,
"stream": "cu351.alarm_code"
}, {
"id": 51677,
"timestamp": "2017-08-16T13:59:57Z",
"type": "alarm",
"code": 214,
"title": "",
"description": "",
"remedy": "",
"appeared": "2017-08-16T13:59:59Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Demo 07122016",
"system_id": 693,
"system_device_id": 371,
"stream": "cu351.alarm_code"
}
]
}
答案 0 :(得分:5)
Array#filter
期望一个布尔值作为返回值,你可以使用这个
let myKeys = Object.keys(data).filter(key => key == vm.system_id);
获取密钥,然后使用给定的密钥渲染新对象。
要获取单个数组中的所有项目,您可以使用
收集它们let result = myKeys.reduce((r, k) => r.concat(data[k]), []);
答案 1 :(得分:1)
如果你有正确的钥匙,你可以从对象获得财产。
myKeys.forEach(key => {
console.log(data[key]);
});
这将打印您之前已过滤的键的对象。