今天公司有这么个需求
需要把 一个数组分成多个 [1,2,3,4,5,6,7,8,9,10] 这种数据 然后分成每4个一组(我觉得没啥问题 但是产品说不好看要我分一下)
let list = [1,2,3,4,5,6,7,8,9,10]
let len = list.length
let newList = []
if (len) {
var chunk = 4
for (var i = 0, j = len; i < j; i += chunk) {
console.log(i);
// console.log(j);
// console.log(i += chunk);
let items = list.slice(i, i + chunk)
if (items.length>0) {
// let obj = {
// value0:'',
// value1:'',
// value2:'',
// value3:'',
// }
//这里是因为公司有需要不能用1 2 3这种命名
let obj = {
boxNumberOne:'',
boxNumberTwo:'',
boxNumberThree:'',
boxNumberFour:'',
}
for (let index = 0; index < items.length; index++) {
let name = ''
if (index==0) {
name = 'boxNumberOne'
}else if(index==1){
name = 'boxNumberTwo'
}else if(index==2){
name = 'boxNumberThree'
}else if(index==3){
name = 'boxNumberFour'
}
// for (const key in obj) { //不能用1 2 3这种命名
// if (key=='value'+index) {
// obj[key] = items[index]
// }
// }
for (const key in obj) {
if (key==name) {
obj[key] = items[index]
}
}
}
newList.push(obj)
}
}
}
console.log(newList);//打印结果
如有问题请指教 单纯学习记录
标签:index,obj,key,一组,展示,items,let,数组,name From: https://www.cnblogs.com/hxke/p/17859672.html