首页 > 解决方案 > 嵌套数组未返回预期值

问题描述

我正在尝试访问数组中数组中的单个值,我有整个数组,其中包含数组,然后在其中,我希望能够访问这些值。

我也可能将这些值中的每一个分配给变量。

本质上,我正在尝试使用 JS 遍历将成为值网格(数组中的数组)的内容。

1)我尝试通过将行值作为 JSON 字符串推送到数组中来访问该值,然后将其映射为 array.map => row.map,然后将其映射到单个元素中

2)我已经尝试“解构”对象,但似乎也没有去任何地方。


async function dataFormat(worksheet, rowNumber) {
  csvWorkbook = workbook.csv.readFile("./uploads/uploadedFile.csv");
  await csvWorkbook.then(async function(result) {
    let restarts = 0;
    let nullCounts = true;
    let thermostatRows = []; 

// you have to define THEN destructure the array
    // const [serial,date,startDate,endDate,thing3,thing4,thing5] = firstTemps

    worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
      if (rowNumber > 6) {
        Rows.push(`${JSON.stringify(row.values)}`);

      }
    });
    console.log(thermostatRows[0])
  })
  };

这是我返回第一行的函数。如果我输入 Rows[0][0] 我会收到一封信。

这是 Rows[1] 的返回

[null,"2018-12 03T05:00:00.000Z","16:35:00","heat","heatOff", "hold","Home",70.1,70.1,69.8,43,33.8,0,0,15,15,null,69.8,43,1]

这是有道理的,因为它是第一行。

但是记录 Rows[0][0] 给了我 null 的第一个字母(数组中的第一个值)

最后 ,

[ '[null,"2018-12-02T05:00:00.000Z","23:25:00","heat","heatOff","auto","Home",72,72,72,47,41.3,0,0,0,0,null,72,47,0]',

  '[null,"2018-12-03T05:00:00.000Z","16:35:00","heat","heatOff","hold","Home",70.1,70.1,69.8,43,33.8,0,0,15,15,null,69.8,43,1]',

'[null,"2018-12 03T05:00:00.000Z","16:40:00",null,null,null,null,null,null,null,null,33.8,0]'

如果我在没有其他任何内容的情况下记录行,是近似日志 - 让您了解整个情况。

编辑:我的函数现在看起来像这样,为什么它会返回未定义?

    if (rowNumber > 6) {
      thermostatRows.push((row.values));
    }
  });
  thermostatRows.map(thermostatRow => {
    // let [date,time,SystemSetting,systemMode,calendarEvent,ProgramMode,coolSetTemp,HeatSetTemp,currentTemp,currentHumidity,outdoorTemp,windSpeed,coolStage1,HeatStage1,Fan,DMOffset,thermostatTemperature,thermostatHumidity,thermostatMotion] = thermostatSettings
    console.log(date,time,SystemSetting)
    })
  })
};```

FINAL UPDATE 

It works I figured out the undefined deal myself- try this. (this is with the excel.js library)

  async function dataFormat(worksheet, rowNumber) {
    csvWorkbook = workbook.csv.readFile("./uploads/uploadedFile.csv");
    await csvWorkbook.then(async function(result) {
      let restarts = 0;
      let nullCounts = true;
      let thermostatRows = [];
      let thermostatSettings = []; // you have to define THEN destructure the array
      // const [serial,date,startDate,endDate,thing3,thing4,thing5] = firstTemps

      worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
        if (rowNumber > 6) {
          thermostatRows.push((row.values));
        }
      });
      thermostatRows.map(thermostatRow => { [,date,time,SystemSetting,systemMode,calendarEvent,ProgramMode,coolSetTemp,HeatSetTemp,currentTemp,currentHumidity,outdoorTemp,windSpeed,coolStage1,HeatStage1,Fan,DMOffset,thermostatTemperature,thermostatHumidity,thermostatMotion] = thermostatRow
        console.log(date,time,SystemSetting)
        })
      })
    }; 

标签: javascriptarraysdictionary

解决方案


首先,您需要知道 Javascript 数组是零索引的。这意味着要获得第一项,您使用索引 0。当您调用 Rows[1] 时,您实际上获得了第二项。

其次,您不是在创建二维数组,而是在创建一维字符串数组。

Rows.push(`${JSON.stringify(row.values)}`);

row.values会将其转换为字符串,将其插入另一个字符串,然后将最终结果推送到数组中。

如果您想要第一个数组中的数组,我假设 row.values 包含,请执行Rows.push(row.values);


推荐阅读