Typescript Array push有时删除项



我正在将一些Excel文件上传到服务器以处理数据。我使用xlsx读取和Tabulator在前端显示上传的数据。这是我读取数据并将其压入数组的循环:

for (const k of raw) {
let columns: Column[] = []
//new Array each loop
let tmpData: any[] = []
//read data from Excel files
const wb = read(await k.file.arrayBuffer(), {
sheets: k.type.sheet
})
tmpData = utils.sheet_to_json(wb.Sheets[k.type.sheet], {
range: k.type.row
})
//do something with the data of the files
tmpData.forEach((item, index) => {
if (Object.entries(item).length < 2) {
data.splice(index, 1);
}
});
tmpData.forEach((item) => {
Object.keys(item).forEach((key) => {
if (key.includes('EMPTY')) {
delete item[key]
}
})
})
//seperate the first row to extract columns
Object.keys(tmpData[0]).forEach((key) => {
columns.push(new Column(key, key, 'input', true, 300))
})
//create columns Array containing the headers of all files
columnsObject.push(new ColumnsType(columns, k.type.name))
//push the data into an Array
data.push(new TablesType(tmpData, k.type.name))
//troubleshooting
data.forEach(item => {
console.log(item.vendor)
})
console.log('n ----- n')
}

这是发生问题的故障排除的输出示例:

Ciena
----- 
Ciena
PaloAlto
----- 
PaloAlto
Infinera
----- 
PaloAlto
Infinera
Arista
----- 

在第三个循环中,Ciena对象丢失了。我已经尝试过使用传统的for循环,但是同样的问题出现在一些上传文件的星座上。所有文件的数据读取正常,只是数组把它扔掉了。

//do something with the data of the files
tmpData.forEach((item, index) => {
if (Object.entries(item).length < 2) {
data.splice(index, 1);
}
});

为什么要写这样没有意义的注释呢?这是干什么用的?什么? !

这是btw。也是你最有可能犯错的地方。你在拼接data数组。(意思是从你的数据数组中删除东西)这将解释你的数据数组如何失去它的一些元素。由于我不知道这部分代码实际上是用来做什么的,所以我不能告诉你如何修复它,只是这很可能是你麻烦的根源。

最新更新