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

标签 javascript ecmascript-6

我有一个如下所示的对象 - 如何循环每个 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/

相关文章:

javascript array.slice().push() 未按预期工作

javascript - 如何强制用户按顺序点击div?

java - typescript 枚举的构造函数?

javascript - 传播语法删除

javascript - 使用条件语句设置react-router link prop会返回语法错误

javascript - 如何在 JavaScript 中每天特定时间刷新一组代码

javascript - JQTOUCH,绑定(bind)到通过 AJAX 拉入的链接,进行另一个 AJAX 调用?可能的?

javascript - 访问 Javascript ES6 类中声明的函数(ES2015?ES16?)

javascript - 从选择框中获取所选选项 html

javascript - 使用 ES6 类将实例方法传递给 super