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

时间:2017-08-16 20:25:25

标签: javascript ecmascript-6

我有一个如下所示的对象 - 如何循环每个ID / Keys中的所有项目,并返回所有条目,以便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"
    }
]

}

2 个答案:

答案 0 :(得分:1)

也许是这样的

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

答案 1 :(得分:0)

寻找类似的东西??

如果是这样,不要只是复制/粘贴,而是尝试理解其工作原理和方式。

let jsonObj = {"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"}]}};

let data = [];
Object.keys(jsonObj.system_events).forEach(key => {
jsonObj.system_events[key].forEach(d => data.push(d));
});

console.log(data)