gpt4 book ai didi

javascript - 表行处的 foreach 除以 5

转载 作者:行者123 更新时间:2023-12-03 06:48:29 25 4
gpt4 key购买 nike

如何显示每 5 日创建一个新行的表中的数据 <td> ?

例子

data: [1,2,3,4,5,6];

成分:
<tr v-for="item in data">
<td>{{item}}</td>
<tr>

预期的:

| 1 | 2 | 3 | 4 | 5 |

| 6 |

最佳答案

这是一个易于理解的解决方案,使用 reduce :

new Vue({
el: '#root',
data: {
origin: [1, 2, 3, 4, 5, 6, 7]
},
computed: {
chunkedOrigin: function() {
return this.origin.reduce((result, item, index) => {
const chunkIndex = Math.floor(index/5)

if(!result[chunkIndex]) {
result[chunkIndex] = [] // start a new chunk
}
result[chunkIndex].push(item)

return result;
}, []);
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<table id="root" style="border: 1px solid black">
<tr v-for="row in chunkedOrigin">
<td v-for="column in row" style="border: 1px solid black">{{column}}</td>
</tr>
</table>

关于javascript - 表行处的 foreach 除以 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com