admin管理员组文章数量:1426072
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.
Vue method
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. 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"
. What am I missing?
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.
Vue method
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. 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"
. What am I missing?
-
Is
seat
a string or an object? – Jns Commented Feb 24, 2019 at 14:23 -
Maybe console.log
this.selectedSeats
before callingindexOf
on it will provide a clue – Dexygen Commented Feb 24, 2019 at 14:23 -
well, from the error, I can only assume that
this.selectedSeats
is not an array. What do you get if you doconsole.log(Array.isArray(this.selectedSeats));
beforelet index
? – Nick Parsons Commented Feb 24, 2019 at 14:23 - @Jns it is an Integer – brunouno Commented Feb 24, 2019 at 14:23
-
2
How about logging
this.selectedSeats.indexOf
(should be a native function). Also, isshowSeats
asynchronous? Maybe some more (but not too much) code for context? A jsfiddle or something? – Dexygen Commented Feb 24, 2019 at 14:30
1 Answer
Reset to default 4This is one of those rare cases in JavaScript where leaving off a semicolon can cause huge problems. 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)
Add a semicolon at the end of your indexOf(seat);
and you should be fine.
本文标签: javascriptVue JSindexOf array is not a functionStack Overflow
版权声明:本文标题:javascript - Vue JS - indexOf array is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745459149a2659241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论