VUE-judging whether an array contains a certain value

①findIndex()

['zahngsan','lisi','LIXIUJUAN700','WANGYIBO500'].findIndex((v)=>(v==="LIXIUJUAN700"))
 
// 得到的值!==-1,则存在
 
// 返回2,该值在数组中的位置

②find()

 
let arr =[{name:'ZS'},{name:'WW'},{name:'LS'},{name:'GT'},{name:'JP'},{name:'JP'}];
 
let obj =arr.find((item)=>{item.name==='JP'});
 
if(obj){
// 存在,返回obj={name:'JP'}
}else{
// 不存在
}

③indexOf()

['nts','stg','APP'].indexOf('nts')
 
// === -1 则不存在
 
// !== -1 则存在,返回的是该值在数组的索引 0

④filter()

 
['nts','stg','esg'].filter((m)=>(m!=='stg'));// ['nts','esg']
 
// 可以判断数组过滤后的长度与过滤后的长度比较

⑤includes()

['stg','nts','cds','app'].includes('app');
 
// true      存在
 
// false  不存在

There are many ways, welcome to comment and add

Guess you like

Origin blog.csdn.net/zlfjavahome/article/details/130953104