gpt4 book ai didi

javascript - 循环嵌套数组并过滤掉所有条目?

转载 作者:行者123 更新时间:2023-12-03 03:38:57 25 4
gpt4 key购买 nike

我有一个如下所示的对象 - 如何循环每个 ID/Key 内的所有项目,并返回所有条目,以便在 ES6 中返回单个过滤对象?

我应该看看 .filter/.map/reduce 还是类似 Object.entries 的东西?我已经尝试过 .reduce,但我无法完全理解它:(

{
"system_events": {
"1013": [{
"id": 25899,
"timestamp": "2017-08-15T21:26:42Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:26:40Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Randers pr 44b sidste station"
}, {
"id": 26157,
"timestamp": "2017-08-15T21:32:17Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:32:06Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Randers pr 44b sidste station"
}
],
"1015": [{
"id": 23777,
"timestamp": "2017-08-15T20:38:08Z",
"type": "alarm",
"code": 191,
"title": "",
"description": "",
"appeared": "2017-08-15T20:38:00Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
}, {
"id": 23779,
"timestamp": "2017-08-15T20:38:08Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T20:37:58Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
}
]
}}

希望的输出是:

The example of the output is this: {
[{
"id": 25899,
"timestamp": "2017-08-15T21:26:42Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:26:40Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Randers pr 44b sidste station"
}, {
"id": 26157,
"timestamp": "2017-08-15T21:32:17Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:32:06Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Randers pr 44b sidste station"
}, {
"id": 23777,
"timestamp": "2017-08-15T20:38:08Z",
"type": "alarm",
"code": 191,
"title": "",
"description": "",
"appeared": "2017-08-15T20:38:00Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
}, {
"id": 23779,
"timestamp": "2017-08-15T20:38:08Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T20:37:58Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
}
]

}

最佳答案

也许是这样的

Object.keys(obj.system_events).map((id) => {
return obj.system_events[id];
}).reduce((result, array) => {
return result.concat(array);
}, [])

如果您想添加过滤搜索条件,您可以尝试这样的操作。

let searchCriteria = {
id: 23779,
code: 190
};
Object.keys(obj.system_events).map((id) => {
return obj.system_events[id];
}).reduce((result, array) => {
return result.concat(array);
}, []).filter((obj) => {
// do your filtering here.
let field;
for (field in searchCriteria) {
if (searchCriteria[field] !== obj[field]) {
return false;
}
}
return true;
});

关于javascript - 循环嵌套数组并过滤掉所有条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com