Home >Web Front-end >Vue.js >How to loop an array in vue.js
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.
【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!