简体   繁体   English

在 JavaScript 中遍历嵌套对象和索引数组

[英]Iterate through nested objects and indexed Arrays in JavaScript

I have the following structure:我有以下结构:

0: {
        fulfilled: true
        pending: false
        rejected: false
        data: {
          0: {dateTime: Mon Feb 17 2020 01:00:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          1: {dateTime: Mon Feb 17 2020 01:10:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          2: {dateTime: Mon Feb 17 2020 01:20:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          3: {dateTime: Mon Feb 17 2020 01:30:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          4: {dateTime: Mon Feb 17 2020 01:40:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          5: {dateTime: Mon Feb 17 2020 01:50:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          6: {dateTime: Mon Feb 17 2020 02:00:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          7: {dateTime: Mon Feb 17 2020 02:10:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          8: {dateTime: Mon Feb 17 2020 02:20:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          9: {dateTime: Mon Feb 17 2020 02:30:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
        }
     },
1: {
        fulfilled: true
        pending: false
        rejected: false
        data: {
          0: {dateTime: Mon Feb 17 2020 01:00:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          1: {dateTime: Mon Feb 17 2020 01:10:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          2: {dateTime: Mon Feb 17 2020 01:20:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          3: {dateTime: Mon Feb 17 2020 01:30:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          4: {dateTime: Mon Feb 17 2020 01:40:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          5: {dateTime: Mon Feb 17 2020 01:50:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          6: {dateTime: Mon Feb 17 2020 02:00:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          7: {dateTime: Mon Feb 17 2020 02:10:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          8: {dateTime: Mon Feb 17 2020 02:20:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
          9: {dateTime: Mon Feb 17 2020 02:30:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
        }
     }

I need to iterate through data and its values.我需要遍历数据及其值。

I tried like this:我试过这样:

const data = Object.keys(items).map(key => items[key].data);

but this returns me this value:但这会返回给我这个值:

0: {dateTime: Mon Feb 17 2020 01:00:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
1: {dateTime: Mon Feb 17 2020 01:10:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
2: {dateTime: Mon Feb 17 2020 01:20:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
3: {dateTime: Mon Feb 17 2020 01:30:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
4: {dateTime: Mon Feb 17 2020 01:40:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
5: {dateTime: Mon Feb 17 2020 01:50:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
6: {dateTime: Mon Feb 17 2020 02:00:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
7: {dateTime: Mon Feb 17 2020 02:10:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
8: {dateTime: Mon Feb 17 2020 02:20:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}
9: {dateTime: Mon Feb 17 2020 02:30:00 GMT+0100 (Central European Standard Time), position: Position, address: Address}

Well, after you have your data to iterate through it just use a for loop:好吧,在您拥有要遍历的数据后,只需使用for循环:

const data = Object.keys(items).map(key => items[key].data).flat();
for (item of data) {
    // Do your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM