V for循环增量

时间:2019-02-01 04:26:43

标签: vue.js

<table style="cursor:default">
                    <tbody>
                        <tr v-for="j in status_data.length/2">
                            <td class="w3-border w3-border-black w3-round-large w3-center" :bgcolor="getColor(status_data[j-1].MESSAGE_CODE)">{{ status_data[j-1].PROCESS_CODE }}</td>
                            <td class="w3-border w3-border-black w3-round-large w3-center" :bgcolor="getColor(status_data[j].MESSAGE_CODE)">{{ status_data[j].PROCESS_CODE }}</td>
                        </tr>
                    </tbody>
                </table>

我该如何控制上述代码中j的值,我希望它就像

对于status_data.length / 2中的i 采取行动 i = i + 2

我想在每次迭代中将值增加2。

1 个答案:

答案 0 :(得分:0)

如果要成对使用,请遍历数组并在索引为偶数时收集对。

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]]

请注意,如果数组中的元素数量为奇数,则它将忽略最后一个元素

相关问题