Vue中遍歷數(shù)組的新方法實(shí)例詳解
1、foreach
foreach循環(huán)對(duì)不能使用return來停止循環(huán)
search(keyword){ var newList = [] this.urls.forEach(item =>{ if(item.name.indexOf(keyword) != -1){ newList.push(item) } }) return newList }
2、filter
item對(duì)象就是遍歷數(shù)組中的一個(gè)元素,includes是es6中的新方法,在search方法中直接返回新數(shù)組
search(keyword){ return this.urls.filter(item =>{ if(item.name.includes(keyword)){ return item } }) }
3、findIndex
返回true后index就可以獲取到匹配的元素在進(jìn)行刪除
del(row){ this.$confirm("確定要?jiǎng)h除嗎?", "刪除").then(action=>{ var index = this.urls.findIndex(item =>{ if(item.name == row.name){ return true; } }) this.urls.splice(index, 1) });
4、some
如果匹配成功就return true跳出some的循環(huán)
del(row){ this.$confirm("確定要?jiǎng)h除嗎?", "刪除").then(action=>{ this.urls.some((item, i) =>{ if(item.name == row.name){ this.urls.splice(i, 1) return true; } }) }); }
5、上例子,在一個(gè)vue的data中存入一個(gè)固定的數(shù)組,對(duì)數(shù)組進(jìn)行遍歷,實(shí)現(xiàn)搜索功能,刪除功能
在el-table中 :data中綁定一個(gè)方法,方法中對(duì)固定的數(shù)組urls進(jìn)行遍歷,返回一個(gè)新的數(shù)組實(shí)現(xiàn)搜索功能
<template> <div> <label style="float: left;"> 搜索關(guān)鍵字: <input type="text" class="form-control" v-model="keyword"> </label> <el-table :data="search(keyword)" size="small" :stripe="true" :border="true" @select="select" @select-all="select"> <el-table-column type="selection"></el-table-column> <el-table-column type="index"></el-table-column> <el-table-column label="網(wǎng)站名" prop="name" width="200"> <template slot-scope="slot"> <a href="slot.row.url" target="_blank">{{slot.row.name}}</a> </template> </el-table-column> <el-table-column label="網(wǎng)址" prop="url"></el-table-column> <el-table-column label="類型" prop="type" width="50"></el-table-column> <el-table-column label="國家" prop="country" width="50"></el-table-column> <el-table-column label="操作" width="50"> <template slot-scope="slot"> <el-button size="mini" type="text" icon="el-icon-delete" @click="del(slot.row)"></el-button> </template> </el-table-column> </el-table> <el-divider content-position="left">表格操作</el-divider> <el-button @click="batchDelete" type="danger" icon="el-icon-delete" size="small">批量刪除</el-button> </div> </template> <script> export default { data() { return { keyword:'', selections: [], urls: [{ name: "新浪", url: "http://www.sina.com", type: "資訊", country: "中國" }, { name: "騰訊", url: "http://www.tencent.com", type: "聊天", country: "中國" }, { name: "谷歌", url: "http://www.google.com", type: "資訊", country: "美國" }, { name: "韜睿", url: "http://www.51i-star.com", type: "教育", country: "中國" } ] }; }, methods: { del(row){ this.$confirm("確定要?jiǎng)h除嗎?", "刪除").then(action=>{ /* this.urls.some((item, i) =>{ if(item.name == row.name){ this.urls.splice(i, 1) return true; } }) */ var index = this.urls.findIndex(item =>{ if(item.name == row.name){ return true; } }) this.urls.splice(index, 1) }); }, select(selections, row) { this.selections = selections; }, batchDelete() { this.$confirm("確定要?jiǎng)h除嗎?", "刪除") .then(action => { for (var i = this.urls.length - 1; i >= 0; i--) { for (var j = this.selections.length - 1; j >= 0; j--) { if (this.urls[i].name == this.selections[j].name) { this.urls.splice(i, 1); break; } } } }) .catch(error => { alert(error); this.$message('刪除取消'); }); }, search(keyword){ /* var newList = [] this.urls.forEach(item =>{ if(item.name.indexOf(keyword) != -1){ newList.push(item) } }) return newList */ return this.urls.filter(item =>{ if(item.name.includes(keyword)){ return item } }) } } } </script> <style> </style>
6、效果圖為
總結(jié)
以上所述是小編給大家介紹的Vue中遍歷數(shù)組的新方法實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
vue3封裝一個(gè)帶動(dòng)畫的關(guān)閉按鈕示例詳解
這篇文章主要為大家介紹了vue3封裝一個(gè)帶動(dòng)畫的關(guān)閉按鈕示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09詳細(xì)聊聊前端如何實(shí)現(xiàn)token無感刷新(refresh_token)
實(shí)現(xiàn)token無感刷新對(duì)于前端來說是一項(xiàng)非常常用的技術(shù),其本質(zhì)是為了優(yōu)化用戶體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于前端如何實(shí)現(xiàn)token無感刷新(refresh_token)的相關(guān)資料,需要的朋友可以參考下2022-11-11vee-validate vue 2.0自定義表單驗(yàn)證的實(shí)例
今天小編就為大家分享一篇vee-validate vue 2.0自定義表單驗(yàn)證的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08Vue3實(shí)現(xiàn)跨頁面?zhèn)髦档膸追N常見方法
在Vue 3中,跨頁面?zhèn)髦悼梢酝ㄟ^多種方式實(shí)現(xiàn),具體選擇哪種方法取決于應(yīng)用的具體需求和頁面間的關(guān)系,本文列舉了幾種常見的跨頁面?zhèn)髦捣椒?感興趣的同學(xué)跟著小編來看看吧2024-04-04vue.js中關(guān)于點(diǎn)擊事件方法的使用(click)
這篇文章主要介紹了vue.js中關(guān)于點(diǎn)擊事件方法的使用(click),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue表格首列相同數(shù)據(jù)合并實(shí)現(xiàn)方法
這篇文章主要介紹了Vue實(shí)現(xiàn)表格首列相同數(shù)據(jù)合并的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Element DateTimePicker日期時(shí)間選擇器的使用示例
這篇文章主要介紹了Element DateTimePicker日期時(shí)間選擇器的使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)pdf文檔在線預(yù)覽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11vue3組合式API中setup()概念和reactive()函數(shù)的用法
這篇文章主要介紹了vue3組合式API中setup()概念和reactive()函數(shù)的用法,接下來的事件,我將帶著你從淺到深分析為什么我們需要學(xué)習(xí)組合式API以及我們的setup()函數(shù)作為入口函數(shù)的一個(gè)基本的使用方式,需要的朋友可以參考下2023-03-03