被3个嵌套数组混淆

时间:2018-10-15 20:00:07

标签: javascript arrays typescript

我正在从保存在SharePoint文档库中的excel文件中提取数据。我正在获取行,并且对数据结构感到困惑。我还不太熟悉typscript / javascript。

我要返回行:

private _getRows(documentLibraryID: string, documentID: string, worksheet: string, table: string): Promise<[MicrosoftGraph.WorkbookTableRow]> {
  return new Promise<any>((resolve, reject) => {
    this.props.context.msGraphClientFactory
    .getClient()
      .then((client: MSGraphClient): void => {
        client
          .api(`/sites/${URL}/drives/${documentLibraryID}/items/${documentID}/workbook/worksheets('${worksheet}')/Tables('${table}')/rows`)
          .get((error, response: any, rawResponse?: any) => {
            if (response) {
              let rows:[MicrosoftGraph.WorkbookTableRow] = response.value;

              resolve(rows);
            }
            else {
              reject(error);
            }
          });
        });
  });
}

我得到的回报:

enter image description here

我尝试通过数组枚举:

  for (let row of rows) {
    console.log("row: ", row);
    for (let value of row.values) {
      console.log("value: ", value);
      for (let newValue of value.values) {
        console.log("newValue: ", newValue);

      }
    }
  }

但是我不了解newValue对象:

enter image description here

我对这种结构不了解什么?它看起来像3个嵌套数组。

1 个答案:

答案 0 :(得分:3)

问题在于,当您到达这一点时:for (let value of row.values)value实际上是一个数组。因此,无需执行value.values

此代码应该可以正常工作:

for (let row of rows) {
    console.log("row: ", row);
    for (let valueArr of row.values) {
      console.log("value: ", valueArr);
      for (let newValue of valueArr) {
        console.log("newValue: ", newValue);

      }
    }
  }

希望有帮助!