我有点问题,因此我在登录时有一个带有对象的数组,它显示一切正常,但是当我尝试使用.length
函数时,出于某种原因它返回0
。 / p>
这是我的代码:
async getTicketType(updatedTicketType) {
await this.getTicketTypes();
if (this.typeOptions) {
console.log('ik kom hier');
console.log(this.typeOptions);
console.log(this.typeOptions.length);
for (let i = 0; i < this.typeOptions.length; i++) {
console.log('ik kom hier');
if (this.typeOptions[i]["value"] === updatedTicketType) {
this.currentTypeOptions = this.typeOptions[i];
}
}
}
}
这是日志的图片:
答案 0 :(得分:0)
这是因为您的数组有10个未定义的值,因此它实际上没有length属性,如果您确保每个元素中都有一些值,则可以获取该数组。 这是JS的功能,它是如何计算数组的length属性的。
答案 1 :(得分:0)
您可以尝试再次将其解析为数组:
// arr is an array with contains 10 items
var arr = JSON.parse(JSON.stringify(this.typeOptions));
然后,继续循环...