Home > Web Front-end > Vue.js > How to loop an array in vue.js

How to loop an array in vue.js

coldplay.xixi
Release: 2020-11-17 18:19:25
Original
7316 people have browsed it

Vue.js method to implement loop array: 1. Use for traversal, the code is [for(var i in array):console.log(array[i], i)]; 2. Use forEach method , traverse the array from beginning to end, calling the specified function for each element.

How to loop an array in vue.js

【Recommended related articles: vue.js

vue.js implements loops Array methods:

1. Vue traversal array methods:

var array = [1, 2, 3, 4]
//方法一:
for( var i in array):
    console.log(array[i], i)
 
//方法二:
array.forEach((v, i) => {
    console.log(v, i)
})
Copy after login

for loop and forEach are equivalent, both loop arrays.

2. The forEach() method traverses the array from beginning to end and calls the specified function for each element.

var a = [1,2,3,4,5];     
var sum = 0;             
a.forEach(function (value) {
    sum += value
})             
console.log(sum); //sum = 15
Copy after login

Related free learning recommendations: javascript (video)

The above is the detailed content of How to loop an array in vue.js. 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