嵌套数组未返回期望值

时间:2019-04-05 02:24:48

标签: javascript arrays dictionary

我试图访问数组中数组中的各个值,我拥有一个整体数组,该数组中包含数组,然后在其中,我希望能够访问这些值。

我可能还会将每个值分配给变量。

本质上,我试图使用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,"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]'

如果我不另外记录行,就是大约的对数-让您了解整个情况。

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

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

1 个答案:

答案 0 :(得分:0)

首先,您需要了解Javascript数组是零索引的。这意味着要获取第一个项目,请使用索引0。调用Rows [1]时,实际上就是第二个项目。

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

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

这需要row.values并将其转换为字符串,将其插值为另一个字符串,然后将最终结果压入数组。

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

相关问题