typescript - Iterating a Multidimensional Array in TypeScript
问题描述
I am having trouble iterating through a multidimensional array in TypeScript. In order to store the data into a db, I need to convert the multidimensional array into a one dimensional one.
My function:
storeDevices() {
let tempDeviceList: Device[][] = this.dataStorageService.getDevices();
console.log(tempDeviceList);
console.log(tempDeviceList[1]);
console.log(tempDeviceList[1][1]);
console.log(tempDeviceList.length);
}
console.log(tempDeviceList);
results in https://pastebin.com/mb2B9yrM I am using this as a lookup table which is why the first element is often null.
I do not understand why
console.log(tempDeviceList[1]); //undefined
console.log(tempDeviceList[1][1]); //undefined
console.log(tempDeviceList.length); //0
result in undefined and 0. Because of this I am not able to iterate over the array. Based on the printed JSON, those elements should exist.
解决方案
Converting a multi-dimensional array into a single dimensional one is also known as array flattening. There's plenty of resources on this topic, here's one on SO to get you started.
推荐阅读
- javascript - 替换时 gulp dest.on 不是函数
- c# - Mono C#:意外符号
- python - Python XLSB 到 CSV 转换数据类型
- c++ - 在 C++ 项目中使用基于 CUDA 的库代码
- ios - 制作自定义形状按钮 - Swift
- python-3.6 - Apache Airflow + Python 3.6 + Local Executor + Mysql 作为元数据库
- android - GridView 使我的应用程序崩溃 - 如何解决?
- xamarin - 更改加速度会在 Movesense for Xamarin 上提供 BAD_REQUEST
- xamarin - 使用 FlexLayout 换行,如何在行之间留出空间?
- python - 合并两个数据框的列而不重复行