[Vuejs]-V for loop incremental

0👍

If you want it by pairs, go through the array and collect pairs whenever the index is an even number.

status_data
.map((v, i) => i%2 === 0 ? arr.slice(i, i+2) : [])
.filter(x => x.length > 0)
console> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
.map((v, i) => i%2 === 0 ? arr.slice(i, i+2) : [])
.filter(x => x.length > 0)

// will return an array like [[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]]

Beware that this will ignore the last element if your array has an odd number of elements

Leave a comment