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

时间:2020-03-30 19:37:49

标签: javascript typescript

我具有以下结构:

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

我需要遍历数据及其值。

我尝试过这样:

const data = Object.keys(items).map(key => items[key].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 个答案:

答案 0 :(得分:0)

好吧,在您要遍历数据之后,只需使用for循环即可:

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