直接看代码
mounted(){
let arr = [1,2,3,4,5,6,7,8,9,10,11,12]
console.log(this.divideEqually(arr))//[[1,2,3],[4,5,6],[7.8.9],[10,11,12]]
},
methods:{
divideEqually(originArr) {
/*此函数用于,把数组平均分成4份。
* originArr,带分割的数组。*/
if (originArr.length >= 4) {
let arr1, arr2, arr3, arr4;
arr1 = originArr.slice(0, Math.round(originArr.length / 4));
arr2 = originArr.slice(Math.round(originArr.length / 4), Math.round(originArr.length / 4) * 2);
arr3 = originArr.slice(Math.round(originArr.length / 4) * 2, Math.round(originArr.length / 4) * 3);
arr4 = originArr.slice(Math.round(originArr.length / 4) * 3);
return [arr1, arr2, arr3, arr4];
}
},
}