gpt4 book ai didi

vue.js - 如何在 vue.js 2 上循环对象观察器?

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:31 24 4
gpt4 key购买 nike

如果我console.log(this.list),结果如下:

enter image description here

this.list.forEach(function (user) {
selected.push(user.id);
});

存在错误:

Uncaught TypeError: this.list.forEach is not a function

我该如何解决这个错误?

最佳答案

this.list 不是数组吗?

如果 this.list 是类数组(该对象上必须有一个 length 属性),您应该能够:

Array.prototype.forEach.call(this.list, user => {
// ...
})

Array.from(this.list).forEach(user => {
// ...
})

[...this.list].forEach(user => {
// ...
})

否则,如果 this.list 只是一个普通对象,您可以:

Object.keys(this.list).forEach(key => {
const user = this.list[key]
// ...
})

Object.entries(this.list).forEach(([key, user]) => {
// ...
})

关于vue.js - 如何在 vue.js 2 上循环对象观察器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47388040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com