
今天分享一个使用 Javascript 分割数组为多个自数组的方法实现。我使用它的场景如下:
给定一个数组 arr 和指定大小 fixed:
const arr = [ { id: 1, name: 'name1' }, { id: 2, name: 'name2' }, { id: 3, name: 'name3' }, { id: 4, name: 'name4' }, { id: 5, name: 'name5' }, { id: 6, name: 'name6' }, { id: 7, name: 'name7' }, { id: 8, name: 'name8' }, { id: 9, name: 'name9' } ] const fixed = 2; 期望的结果是生成一个数组,数组中包含 5 个数组,如下:
[ [ { id: 1, name: 'name1' }, { id: 2, name: 'name2' } ], [ { id: 3, name: 'name3' }, { id: 4, name: 'name4' } ], [ { id: 3, name: 'name3' }, { id: 6, name: 'name6' } ], [ { id: 3, name: 'name3' }, { id: 6, name: 'name6' } ], [ { id: 3, name: 'name3' }, {} ] ] 按照 fixed 的大小分割,如果遇到不够 fixed 大小的,使用空对象填充。这种场景对表格数据填充需要等宽或者等数量会有所帮助。 具体实现代码如下:
/** * * @param {arr} 要分割的数组 * @param {fixed} 指定分割的大小 **/ function splitArr(arr, fixed) { let result = []; let size = arr.length; let len = Math.ceil(arr.length / fixed);//向上取整 for(let i=0; i<len; i++){ let tempArr = []; for(let j=0; j<fixed; j++){ if((i*fixed)+j >= size){ tempArr[j] = {} }else{ tempArr[j] = arr[j]; } } result.push(tempArr); if(arr.length > 0){ arr.splice(i, fixed); } } return result; } const arr = [ { id: 1, name: 'name1' }, { id: 2, name: 'name2' }, { id: 3, name: 'name3' }, { id: 4, name: 'name4' }, { id: 5, name: 'name5' }, { id: 6, name: 'name6' }, { id: 7, name: 'name7' }, { id: 8, name: 'name8' }, { id: 9, name: 'name9' } ] const result = splitArr(arr, 2); console.log(result); 希望本次分享的代码对你有所帮助,Thanks !!!
1 StrangerA 2024-05-08 10:36:36 +08:00 为什么例子里分割后 id 为 3 的出现了 4 次,为 6 的出现了两次? |
2 xjngbla 2024-05-08 10:45:06 +08:00 没什么用,但是谢谢你 |
3 hay0577 2024-05-08 10:57:21 08:00 function splitArr(arr, fixed) { var index = 0; var arrayLength = arr.length; var result = []; for (index = 0; index < arrayLength; index += fixed) { var tempArr = arr.slice(index, index+fixed); if(tempArr.length<fixed){ tempArr.splice(tempArr.length+1, 0, ...new Array(fixed-tempArr.length).fill({})); } result.push(tempArr); } return result; } var result = splitArr([{name:'1'},{name:'2'},{name:'3'},{name:'4'},{name:'5'},{name:'6'},{name:'7'},{name:'8'},{name:'9'}], 4); console.log(result); 我的只有一个 for 循环。。 |
4 StrangerA 2024-05-08 10:58:10 +08:00 ``` import { groupBy } from 'lodash' const arr = ... const fixed = 2 const group = groupBy(arr, (item) => Math.floor((item['id'] + 1) / fixed)) const newArr = Object.values(group).map((item) => new Array(fixed).fill({}).map((_, index) => item[index] || {}), ) console.log(newArr) ``` 执行结果: ``` [ [ { id: 1, name: 'name1' }, { id: 2, name: 'name2' } ], [ { id: 3, name: 'name3' }, { id: 4, name: 'name4' } ], [ { id: 5, name: 'name5' }, { id: 6, name: 'name6' } ], [ { id: 7, name: 'name7' }, { id: 8, name: 'name8' } ], [ { id: 9, name: 'name9' }, {} ] ] ``` op 你要的是这样的吗 |
5 icandoallthings 2024-05-08 11:03:30 +08:00 之前写过一个将数组 array 分割成多个长度为 chunkSize 的子数组 splitArray(array, chunkSize = 500) { const result = []; const length = array.length; for (let i = 0; i < length; i += chunkSize) { result.push(array.slice(i, i + chunkSize)); } return result; }, |
6 Leviathann 2024-05-08 11:10:14 +08:00 reduce 一下就行了 |
7 vvhy 2024-05-08 11:14:18 +08:00 ``` reshape = (a,dim,i=0,d=0)=>dim[d] ? Array(dim[d]).fill().map((_,j)=>reshape(a, dim, i * dim[d] + j, d + 1)) : a[i]; reshape([...Array(24).keys()], [2,3,4]) // [[[0,1,2,3],[4,5,6,7],[8,9,10,11]],[[12,13,14,15],[16,17,18,19],[20,21,22,23]]] ``` |
9 iyiluo 2024-05-08 11:29:37 +08:00 先把数组填充到可以整除的倍数,然后直接分割就行了 |
11 kneo 2024-05-08 11:36:57 +08:00 逻辑问题就不说了,自己修去。提一个要注意的地方:返回新数组就不要修改原数组。 |
13 xiaohupro OP @StrangerA 感谢提醒,刚刚看了一下,应该将 splice(i, fixed) 改为 splice(0, fixed),因为每次删除时候应该从头开始,手残了,抱歉 |
15 xiaohupro OP @kneo 逻辑上 splice 那里确实出了问题,已 append ,感谢。这种写法确实修改了原始数组,不是太好 |
16 StrangerA 2024-05-08 12:02:25 +08:00 @huhailong1121 lodash 提供那些功能在某些语言里都是标准库能提供的存在了。简单的数组操作安心用 lodash 就成,不要浪费时间手搓,把省下来的时间用来写更重要的东西(甚至用来摸鱼都比手搓浪费时间强) |
19 otakustay 2024-05-08 13:41:10 +08:00 GPT 整出来的: function chunk(array, size) { return Array.from({ length: Math.ceil(array.length / size) }, (_, index) => array.slice(index * size, index * size + size) ); } |
20 forty 2024-08-26 21:51:18 +08:00 |