如何检查ticketId
是否已经插入到数组中?
这是代码:
state = { myState: [], status: 0, limit: 5 };
selected(ticketId) {
if (this.state.status < this.state.limit) {
this.state.myState.push({ id: ticketId }); //what ever i chose get inserted
this.state.status++;
this.setState({ status: this.state.status });
//console.log('here', this.state.status);
} else {
console.log('3'); //check if condition meet
}
}
如果 ticketId
已经插入并触发警报,我如何创建条件检查 myState
数组?
最佳答案
当您存储为对象 {id:value} 时,最好改用“every”。
state = { myState: [], status: 0, limit: 5 };
selected(ticketId) {
if (this.state.status < this.state.limit ) {
if(this.state.myState.every((item) => item.id !== ticketId)){
this.state.myState.push({ id: ticketId }); //what ever i chose get inserted
this.state.status++;
this.setState({ status: this.state.status });
//console.log('here', this.state.status);
}
} else {
console.log('3'); //check if condition meet
}
}
关于javascript - React-native 数组检查值是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46417479/