目錄 Vue判斷數組內是否存在某一項 兩種方法: findIndex()和 indexOf()方法 VUE-判斷數組中是否含有某個值 ①findIndex() ②find() ③indexOf() ④filter() ⑤includes() Vue判斷數組
目錄
- Vue判斷數組內是否存在某一項
- 兩種方法:
- findIndex()和 indexOf()方法
- VUE-判斷數組中是否含有某個值
- ①findIndex()
- ②find()
- ③indexOf()
- ④filter()
- ⑤includes()
Vue判斷數組內是否存在某一項
兩種方法:
findIndex()和 indexOf()方法
findIndex()顧名思義,查找符合條件的值并返回其索引(返回值為-1表示不存在滿足條件的值),通過判斷返回值對其進行下一步操作
indexOf()從頭開始尋找是否存在符合條件的字符串,返回值為-1表示不存在
//方法一:通用 xx(Arr,date){ // 返回值等于-1 說明數組Arr中不存在id為date的對象 if( Arr.findIndex(item => item.id=== date )!==-1){ ... } } //方法二:當數組里的對象為字符串時用這個方法更簡單 xx(Arr,date){ // 返回值等于-1 說明數組Arr中不存在id為date的對象 if( Arr.indexOf(date)!==-1 ){ ... } }
實例
xxx(){ const that=this; that.$axios.get('/get_collection_user') //axios請求 .then((res)=>{ that.cards = res.data //獲取cards數組 //判斷數組內是否存在數據that.storeId,如果不存在返回值為-1 if(that.cards.findIndex(item => item.mindId=== that.storeId)!==-1){ that.isActive = true } })
VUE-判斷數組中是否含有某個值
①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 不存在
到此這篇關于Vue判斷數組內是否存在某一項的文章就介紹到這了,更多相關Vue判斷數組內是否存在某一項內容請搜索技圈網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持技圈網!
【來源:國外高防服務器 http://www.558idc.com/stgf.html 歡迎留下您的寶貴建議】聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。