将数组分组
function chunk( arr, size) { let _arr = arr; let _size = size; let length = _arr.length; _size = Math.max(toInteger(_size), 0); if (!Array.isArray(_arr) || size <= 0) { return []; } if (length < _size) { return _arr; } let result = []; for (let index = 0, len = Math.ceil(length / _size); index < len; index++) { result.push(_arr.slice(_size * index, _size * (index + 1))); } return result; } // 转换成整数 function toInteger(num) { let _num = +num; if (isNaN(_num)) { return 0; } if (_num === Infinity || _num === -Infinity) { const sign = (_num < 0 ? -1 : 1); return sign * Number.MAX_VALUE; } if (_num < 0) { _num = Math.ceil(_num); } else { _num = Math.floor(_num); } return _num; } let obj = [ { 'age' : 'kkk', }, 1, 2, undefined, null, [1, 2, 3], ]; console.log(chunk(obj, 4));