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

How to loop an array in vue.js

coldplay.xixi
coldplay.xixiOriginal
2020-11-09 15:33:477316browse

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)
})

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

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!

Statement:
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

Related articles

See more