Bootstrap

将一个数组拆分为相同长度的多个数组

let arrSliceSameLength = function(orgArr, size) { size = parseInt(size); if (isNaN(size) || size < 1 || size >= orgArr.length) { return orgArr; } let newArr = []; for (let i = 0, len = orgArr.length; i < len; i += size) { newArr.push(orgArr.slice(i, i+size)); } return newArr; } //example: let arr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; arrSliceSameLength(arr, -1); arrSliceSameLength(arr, 0); arrSliceSameLength(arr, 5); arrSliceSameLength(arr, 9.9); arrSliceSameLength(arr, 26); arrSliceSameLength(arr, 'doorgod'); //知识点 Array.prototype.slice(); //slice() 方法返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新数组对象,原始数组不会被修改 //arr.slice(begin, end); //begin,end参数可选,为负数表示从原数组倒数第几个开始或结束。结果包含begin,不包含end String.prototype.slice() //用法类似
实际应用场景为后端返回一个相同属性列表需要拆分显示

转载于:https://www.cnblogs.com/doorgod/p/7875117.html

;