首页 > 解决方案 > 在Vue中初始化一个对象数组

问题描述

我确实使用数组中的对象来读取它,它总是有效,但不知何故它不能用于初始化它们。我在 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
                    })
}

标签: vue.jsapexcharts

解决方案


您应该将当前对象与以下artNr属性合并:

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

或者

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

推荐阅读