【发布时间】:2021-12-09 23:36:41
【问题描述】:
我的状态包含以下属性:
sentences: [
{
text: "Go away and hide behind that door where we found you just now.",
grade: 606
},
{
text: "Please don't let anyone spoil these nice fresh flowers.",
grade: 609
},
]
现在我正在尝试迭代句子,在每个句子上创建一个名为 words 的新属性,这将是一个单词数组。当我 console.log(this.sentences) 时,我看到了新属性,但是当我尝试在 DOM 中呈现该属性时,它没有显示新属性。
mounted(){
this.sentences.map((sentence) => {
const arrayOfWords = sentence.text.split(' ');
sentence.words = arrayOfWords
})
console.log(this.sentences)
}
【问题讨论】:
-
你需要
let words = this.sentences.map(...) -
@mplungjan 为什么将它存储在变量中会改变什么?我只是想改变原始数组。
-
Array.map()返回一个新数组。您需要将其存储在this.sentences -
但是为什么 console.log(this.sentences) 显示我已经对其进行了变异并添加了单词数组?
标签: javascript vue.js vuejs2