2017-08-16 83 views
0

我有一個看起來像下面的對象 - 我如何循環每個ID/Keys中的所有項目,並返回所有條目,以便在ES6中單個過濾對象?循環嵌套數組並過濾掉所有條目?

我應該看看.filter/.map/reduce還是像Object.entries之類的東西?我試過。降低,但它周圍:(

{ 
"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

是你應該嘗試 – webdeb

+0

你可以給你的預期輸出格式的例子 –

+0

當然,我想收集各「ID /號碼」內的所有物品,都擁有這一切在過濾的對象......使感? – nuffsaid

回答

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)

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