Home > Web Front-end > Vue.js > body text

How vue.js traverses an array

coldplay.xixi
Release: 2020-11-26 15:24:01
Original
12856 people have browsed it

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 =>】.

How vue.js traverses an array

  • ##This method is suitable for all brands of computers


vue.js method of traversing arrays:

1, foreach

Return cannot be used for foreach loops To stop the loop

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

2、filter

The item object is an element in the traversed array, and includes is a new method in es6. The search method directly returns the new array

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

3, findIndex

After returning true, the index can obtain the matching elements and delete them

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

4, some

If the match is successful, return true and jump out of the some loop

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

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



Copy after login

6. The rendering is

How vue.js traverses an array

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!