vue一维数组变为二维数组的方法
当遇到需要将一维数组变为二维数组遍历时的方法,适用于vue2,uniapp,vue3vue2:data(){size:4}computed: {pages() {const pages = []this.swiperList.forEach((item, index) => {const page = Math.floor(inde...
当遇到需要将一维数组变为二维数组遍历时的方法,适用于vue2,uniapp,vue3
vue2:
data(){
size:4
}
computed: {
pages() {
const pages = []
this.swiperList.forEach((item, index) => {
const page = Math.floor(index / this.size)
if (!pages[page]) {
pages[page] = []
}
pages[page].push(item)
})
return pages
}
vue3:
let size = 4;
let pages = computed(() => {
let pages = [] as any[];
list.forEach((item, index) => {
let page = Math.floor(index / size);
if (!pages[page]) {
pages[page] = [];
}
pages[page].push(item);
});
return pages;
});
更多推荐
所有评论(0)