vue.js - 在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
})
}
解决方案
您应该将当前对象与以下artNr
属性合并:
this.articleList[i] = {...this.articleList[i], artNr: e}
或者
Object.assign(this.articleList[i], { artNr: e })
推荐阅读
- javascript - Vuex axios调用无法处理422响应
- php - Mac M1 无法将 PHP 8.0 映射为 PhpStorm 中的 CLI 解释器
- sql - 数据连接向导未加载
- java - Rest Template Exchange 不在 docker 容器内工作,但在外部工作
- python - 如何合并从word文档中读取的表中的行?
- r - 如何在列表 R 中使用 group_by(或类似命令)
- python - 将单个列表添加到字典中,
- python - Openpyxl:如何获取特定列的值?
- python - 在 Pandas 中获取频率和运行总和、运行总和百分比?
- parsing - 如何反驳 LL(3) 的语法?