在javascript中使用分割功能后如何推送变量?

问题描述 投票:0回答:2

错误:未捕获(承诺)TypeError:无法读取属性未定义错误行的“推送”:“ this.name [i] .push(arrayData [0]);“

我不明白,因为console.log(“ data is loading:” + arrayData [0]);正在工作!

关于异步吗?有人可以帮我吗?

这是我的代码:

 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]);
            }
          }
        });
    },
javascript vue.js split push
2个回答
0
投票

通过这种方式this.name[i].push(arrayData[0]);,您试图将一个元素推到另一个元素中,这就是为什么会出现该错误。

[this.name是制表符,this.name[i]是一个元素,因此应为this.name.push(arrayData[0]);


0
投票

名称数组中没有任何元素,因此您应该像这样进行推送。

this.name.push(arrayData[0]);
© www.soinside.com 2019 - 2024. All rights reserved.