[英]How do i push a variable after using split function in javascript?
error: Uncaught (in promise) TypeError: Cannot read property 'push' of undefined error line: " this.name[i].push(arrayData[0]); "错误:未捕获(承诺)类型错误:无法读取未定义错误行的属性“推送”:“ this.name[i].push(arrayData[0]);”
I do not understand since the line before console.log("data is loaded:" + arrayData[0]);我不明白,因为 console.log("data is loaded:" + arrayData[0]); is working!正在工作!
Is it something about async?是关于异步的吗? Can someone please help me out?有人可以帮我吗?
Here is my code:这是我的代码:
data: {
name: []
},
methods: {
LoadData: function() {
console.log("onload fundtion. \n");
fetch('http://localhost/store/skininfor.txt')
.then(response => response.text())
.then((data) => {
// console.log(data);
var textByLine = data.split("\n");
for (var i = 0; i < textByLine.length; i++) {
var arrayData = textByLine[i].split(",");
console.log("data is loaded:" + arrayData[0]);
if (arrayData[0] !== undefined) {
this.name[i].push(arrayData[0]);
}
}
});
},
By this way this.name[i].push(arrayData[0]);
通过这种方式this.name[i].push(arrayData[0]);
you are trying to push an element into another element this is why you have that error.您正试图将一个元素推入另一个元素,这就是您遇到该错误的原因。
this.name
is the tab and this.name[i]
is one element so it should be this.name.push(arrayData[0]);
this.name
是选项卡, this.name[i]
是一个元素,所以它应该是this.name.push(arrayData[0]);
您在名称数组中没有任何元素,因此您应该像这样推送。
this.name.push(arrayData[0]);
You probably need to assign instead of pushing, ie您可能需要分配而不是推送,即
this.name[i] = arrayData[0];
(Although I can't be sure. If you defined example input data and desired output, that would be helpful). (虽然我不能确定。如果你定义了示例输入数据和所需的输出,那会很有帮助)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.