javascript - 嵌套数组未返回预期值
问题描述
我正在尝试访问数组中数组中的单个值,我有整个数组,其中包含数组,然后在其中,我希望能够访问这些值。
我也可能将这些值中的每一个分配给变量。
本质上,我正在尝试使用 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)
})
})
};
解决方案
首先,您需要知道 Javascript 数组是零索引的。这意味着要获得第一项,您使用索引 0。当您调用 Rows[1] 时,您实际上获得了第二项。
其次,您不是在创建二维数组,而是在创建一维字符串数组。
Rows.push(`${JSON.stringify(row.values)}`);
这row.values
会将其转换为字符串,将其插入另一个字符串,然后将最终结果推送到数组中。
如果您想要第一个数组中的数组,我假设 row.values 包含,请执行Rows.push(row.values);
推荐阅读
- python - 当达到 2000000 个令牌时,有没有办法停止在 gensim.WikiCorpus 中创建词汇?
- python - 如何在 Python 的另一个线程中从异常中删除打印输出
- java - 循环遍历地图内的 3 个列表
- javascript - 具有不同 ID 的多个按钮到一个变量
- reactjs - 如何在 React 中使用来自 GET 请求的数据填充用户变量
- python - 自定义异常 python + Sentry stacktrace
- php - 在插件设置页面启用插件功能
- xamarin.forms - Xamarin 表单:如何使时间选择器不允许选择过去的时间
- php - 违反完整性约束:1052
- excel - VB.NET 从 excel 中获取特定的单元格值