在Vue中初始化对象数组

时间:2020-01-21 18:30:35

标签: vue.js apexcharts

我确实使用Arrays中的Objects进行读取,并且始终可以工作,但是以某种方式无法初始化它们。我需要在articleList里面包含更多的变量,然后仅是数字,所以我不能只将值赋予普通数组。

这有效:

data(){
        return{
            articleList:[],
      }
}

in the method {
  number.forEach((e,i)=>{
                        this.articleList[i] = e
                    })
}

这在某种程度上是行不通的:

 data(){
            return{
                articleList:[
                    {artNr:null}
                    ],
}
}
 in the method{
 number.forEach((e,i)=>{
                        this.articleList[i].artNr = e
                    })
}

1 个答案:

答案 0 :(得分:0)

您应将当前对象与artNr属性合并,如下所示:

this.articleList[i] = {...this.articleList[i], artNr: e}

Object.assign(this.articleList[i], { artNr: e })