[英]Vue JS - indexOf array is not a function
I'm trying to check if a number is present in an array (which I've done a thousand times before using .indexOf()
) but I seem to be missing something now. 我正在尝试检查数组中是否存在数字(使用
.indexOf()
之前已经完成了一千次了),但现在似乎丢失了一些东西。
Vue method Vue方法
showSeat(seat) {
if( !this.selectedSeats.length ) {
this.selectedSeats.push(seat)
} else {
let index = this.selectedSeats.indexOf(seat)
( index >= 0 ) ? this.selectedSeats.splice(index,1) : this.selectedSeats.push(seat)
}
}
Initially, this.selectedSeats
is equal to []
, and the first condition runs perfectly. 最初,
this.selectedSeats
等于[]
,并且第一个条件完美运行。 However, when I try to add another seat , I get [Vue warn]: Error in event handler for "showSeat": "TypeError: this.selectedSeats.indexOf(...) is not a function"
. 但是,当我尝试添加另一个座位时 ,出现
[Vue warn]: Error in event handler for "showSeat": "TypeError: this.selectedSeats.indexOf(...) is not a function"
。 What am I missing? 我想念什么?
This is one of those rare cases in JavaScript where leaving off a semicolon can cause huge problems. 这是JavaScript中罕见的情况之一,其中省略分号会导致巨大的问题。 These two lines are being evaluated as a single expression:
这两行作为单个表达式求值:
let index = this.selectedSeats.indexOf(seat)
( index >= 0 ) ? this.selectedSeats.splice(index,1) : this.selectedSeats.push(seat)
It's trying to execute this.selectedSeats.indexOf(seat)(index>=0)
它正在尝试执行
this.selectedSeats.indexOf(seat)(index>=0)
Add a semicolon at the end of your indexOf(seat);
在
indexOf(seat);
的末尾添加分号indexOf(seat);
and you should be fine. 你应该没事的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.