Arrays 角度7和数组将两个数组推到一个数组中会产生未定义的错误
我需要将从excel文件中读取的两个数组合并到二维数组中,这样我就可以将其作为物料表的Arrays 角度7和数组将两个数组推到一个数组中会产生未定义的错误,arrays,angular,typescript,angular7,Arrays,Angular,Typescript,Angular7,我需要将从excel文件中读取的两个数组合并到二维数组中,这样我就可以将其作为物料表的数据源 我使用XLSX库从excel读取了这两个数组: reader.onload = (e) => { const res = reader.result as string; // This variable contains your file as text const lines = res.split('\n'); // Splits you file into
数据源
我使用XLSX
库从excel读取了这两个数组:
reader.onload = (e) => {
const res = reader.result as string; // This variable contains your file as text
const lines = res.split('\n'); // Splits you file into lines
let ids=[];
let name = [];
let array:any[][];
lines.forEach((line, index) => {
//console.log(line);
ids.push((line.split(',')[0]));
name.push(line.split(',')[1]);
array.push([ids, name])
});
console.log(array);
}
但是我在console.log(array)
上不断收到一个错误:
错误类型错误:无法读取未定义的属性“push”
编辑:
我将代码更改为:
let name = [];
lines.forEach((line, index) => {
//console.log(line);
ids.push((line.split(',')[0]));
name.push(line.split(',')[1]);
array.push(ids, name)
});
console.log(array);
结果是:
但这不是我所需要的,因为它不能作为物料表上的数据源。如果我正确地解决了您的问题。。。以下是实现您的目标的简单方法 要求
let line=[“1,xx”,“2,yy”,“3,zz”];
让name=[];
让数组=[];
行。forEach((行,索引)=>{
//控制台日志(行);
设obj={
id:line.split(',')[0],
名称:line.split(',')[1]
};
//id.push((line.split(',')[0]);
//name.push(line.split(',')[1]);
array.push(obj)
});
console.log(数组)代码>您的数组没有值bro。我想你应该像这样为你的数组分配空数组:让数组:any[]=[]
@stwilz请检查我在问题末尾的编辑,只是为了正确的标记-这是一个纯Javascript/Typescript问题。与角的或不相关的xlsx@everyBit请检查问题末尾的编辑