Vue.js method of traversing arrays: 1. Use a foreach loop, the code is [this.urls.forEach(item =>]; 2. Use a filter loop, the code is [return this.urls.filter (item =>】.
- ##This method is suitable for all brands of computers
vue.js method of traversing arrays:
1, foreach
search(keyword){ var newList = [] this.urls.forEach(item =>{ if(item.name.indexOf(keyword) != -1){ newList.push(item) } }) return newList }
2、filter
search(keyword){ return this.urls.filter(item =>{ if(item.name.includes(keyword)){ return item } }) }
3, findIndex
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
del(row){ this.$confirm("确定要删除吗?", "删除").then(action=>{ this.urls.some((item, i) =>{ if(item.name == row.name){ this.urls.splice(i, 1) return true; } }) }); }5. In the above example, in Store a fixed array in a vue data, traverse the array, implement search function, delete function In el-table: bind a method in data, and perform fixed array urls in the method Traverse and return a new array to implement the search function
<template> <div> <label style="float: left;"> 搜索关键字: <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>6. The rendering is
Related free learning recommendations:javascript(video)
The above is the detailed content of How vue.js traverses an array. For more information, please follow other related articles on the PHP Chinese website!

VueUse 是 Anthony Fu 的一个开源项目,它为 Vue 开发人员提供了大量适用于 Vue 2 和 Vue 3 的基本 Composition API 实用程序函数。本篇文章就来给大家分享几个我常用的几个 VueUse 最佳组合,希望对大家有所帮助!

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

本篇文章给大家整理分享8个GitHub上很棒的的 Vue 项目,都是非常棒的项目,希望当中有您想要收藏的那一个。

如何使用VueRouter4.x?下面本篇文章就来给大家分享快速上手教程,介绍一下10分钟快速上手VueRouter4.x的方法,希望对大家有所帮助!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
