温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Vue中遍历数组

发布时间:2021-05-20 17:49:50 来源:亿速云 阅读:546 作者:Leah 栏目:web开发

本篇文章给大家分享的是有关如何在Vue中遍历数组,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1、foreach

  foreach循环对不能使用return来停止循环

 search(keyword){      var newList = []      this.urls.forEach(item =>{       if(item.name.indexOf(keyword) != -1){        newList.push(item)       }      })      return newList     }

2、filter

  item对象就是遍历数组中的一个元素,includes是es6中的新方法,在search方法中直接返回新数组

 search(keyword){      return this.urls.filter(item =>{       if(item.name.includes(keyword)){        return item     }   })  }

3、findIndex

  返回true后index就可以获取到匹配的元素在进行删除

del(row){      this.$confirm("确定要删除吗?", "删除").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的循环

del(row){     this.$confirm("确定要删除吗?", "删除").then(action=>{       this.urls.some((item, i) =>{       if(item.name == row.name){        this.urls.splice(i, 1)        return true;       }      })    }); }

5、上例子,在一个vue的data中存入一个固定的数组,对数组进行遍历,实现搜索功能,删除功能

  在el-table中 :data中绑定一个方法,方法中对固定的数组urls进行遍历,返回一个新的数组实现搜索功能

<template>   <div>    <label >    搜索关键字:    <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="网站名" 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="网址" 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("确定要删除吗?", "删除").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("确定要删除吗?", "删除")           .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>

Vue的优点

Vue具体轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、虚拟DOM、运行速度快等优势,Vue中页面使用的是局部刷新,不用每次跳转页面都要请求所有数据和dom,可以大大提升访问速度和用户体验。

以上就是如何在Vue中遍历数组,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI