Javascript 根据子数组长度分块数组
Javascript Chunk Array according to subArray length
我对分块子数组有疑问。如下图,detail
里面有很多òbject
。我想将它们 if detail[i].length + detail[i+1].length >= 11
块数组拆分为 2,否则为块 3。
const isQuestions =
[ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] }
, { question: ['ques 2'], detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] }
, { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] }
, { question: ['ques 4'], detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] }
, { question: ['ques 5'], detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] }
, { question: ['ques 6'], detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
, { question: ['ques 7'], detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] }
]
期望的数组是这样的;
[ [ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] }
, { question: ['ques 2'], detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] }
]
, [ { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] }
, { question: ['ques 4'], detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] }
]
, [ { question: ['ques 5'], detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] }
, { question: ['ques 6'], detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
, { question: ['ques 7'], detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] }
] ]
如上所示,预期数组的最后部分的总和为12。但是没关系,重要的是arr[i] + arr[i+1] length
。
我做了一个类似内部地图功能的功能。因为我有多个这样的数组。
function isApsChunk(rawArray, size) {
var returnedArray = [];
for (var i = 0; i < rawArray.length; i += size) {
if (rawArray[i + 1])
if (rawArray[i].detail.length + rawArray[i + 1].detail.length >= 11) {
returnedArray.push(rawArray.slice(i, i + 2));
} else {
returnedArray.push(rawArray.slice(i, i + size));
}
}
return [returnedArray];
}
console.log(isApsChunk(isQuestions, 3))
但问题是函数采用长度为 7 的数组,给我 5。
这样:
在 Array.reduce()
的帮助下
在此代码中,参数 r(结果的首字母缩写词 [doc MDN 中的累加器])
初始化如下:
r = {resp: [], blk: null, len: 0, count: 0}
r.resp
成为最后的累加器
r.blk
指向要填充的子数组
r.len = size
arr细节的积累
r.count = r.blk.length
,这里的线性度由函数的size
参数
得到3
{[i + 1]: next}
允许指向元素 i + 1
并将其命名为 next
,当它在数组外部时他们猜测未定义(当我们在最后一个元素上时,下一个一个不存在并且 !!next
变为 false
!!next
等价于 Boolean(next)
const isQuestions =
[ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] }
, { question: ['ques 2'], detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] }
, { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] }
, { question: ['ques 4'], detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] }
, { question: ['ques 5'], detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] }
, { question: ['ques 6'], detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
, { question: ['ques 7'], detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] }
]
const isApsChunk = (rawArray, size) =>
rawArray.reduce((r,elm,i,{[i+1]:next}) =>
{
if (r.len===0 || r.len >= 11 || r.count >= size)
{
r.len = 0
r.count = 0
r.blk = []
r.resp.push(r.blk )
}
r.len += elm.detail.length
r.count++
r.blk.push( elm )
return (!!next)?r:r.resp
},{ resp:[], blk:null, len:0, count:0 } )
console.log( isApsChunk(isQuestions, 3) )
.as-console-wrapper { max-height: 100% !important; top: 0 }
我对分块子数组有疑问。如下图,detail
里面有很多òbject
。我想将它们 if detail[i].length + detail[i+1].length >= 11
块数组拆分为 2,否则为块 3。
const isQuestions =
[ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] }
, { question: ['ques 2'], detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] }
, { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] }
, { question: ['ques 4'], detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] }
, { question: ['ques 5'], detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] }
, { question: ['ques 6'], detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
, { question: ['ques 7'], detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] }
]
期望的数组是这样的;
[ [ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] }
, { question: ['ques 2'], detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] }
]
, [ { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] }
, { question: ['ques 4'], detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] }
]
, [ { question: ['ques 5'], detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] }
, { question: ['ques 6'], detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
, { question: ['ques 7'], detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] }
] ]
如上所示,预期数组的最后部分的总和为12。但是没关系,重要的是arr[i] + arr[i+1] length
。
我做了一个类似内部地图功能的功能。因为我有多个这样的数组。
function isApsChunk(rawArray, size) {
var returnedArray = [];
for (var i = 0; i < rawArray.length; i += size) {
if (rawArray[i + 1])
if (rawArray[i].detail.length + rawArray[i + 1].detail.length >= 11) {
returnedArray.push(rawArray.slice(i, i + 2));
} else {
returnedArray.push(rawArray.slice(i, i + size));
}
}
return [returnedArray];
}
console.log(isApsChunk(isQuestions, 3))
但问题是函数采用长度为 7 的数组,给我 5。
这样:
在 Array.reduce()
在此代码中,参数 r(结果的首字母缩写词 [doc MDN 中的累加器])
初始化如下:
r = {resp: [], blk: null, len: 0, count: 0}
r.resp
成为最后的累加器
r.blk
指向要填充的子数组
r.len = size
arr细节的积累
r.count = r.blk.length
,这里的线性度由函数的size
参数
{[i + 1]: next}
允许指向元素 i + 1
并将其命名为 next
,当它在数组外部时他们猜测未定义(当我们在最后一个元素上时,下一个一个不存在并且 !!next
变为 false
!!next
等价于 Boolean(next)
const isQuestions =
[ { question: ['ques 1?'], detail: [ {a:1}, {a:2}, {a:3}, {a:4}, {a:5}, {a:6} ] }
, { question: ['ques 2'], detail: [ {b:1}, {b:2}, {b:3}, {b:4}, {b:5}, {b:6}, {b:7}, {b:8} ] }
, { question: ['ques 3?'], detail: [ {c:1}, {c:2}, {c:3}, {c:4}, {c:5}, {c:6}, {c:7} ] }
, { question: ['ques 4'], detail: [ {d:1}, {d:2}, {d:3}, {d:4} ] }
, { question: ['ques 5'], detail: [ {e:1}, {e:2}, {e:3}, {e:4} ] }
, { question: ['ques 6'], detail: [ {f:1}, {f:2}, {f:3}, {f:4} ] }
, { question: ['ques 7'], detail: [ {g:1}, {g:2}, {g:3}, {g:4} ] }
]
const isApsChunk = (rawArray, size) =>
rawArray.reduce((r,elm,i,{[i+1]:next}) =>
{
if (r.len===0 || r.len >= 11 || r.count >= size)
{
r.len = 0
r.count = 0
r.blk = []
r.resp.push(r.blk )
}
r.len += elm.detail.length
r.count++
r.blk.push( elm )
return (!!next)?r:r.resp
},{ resp:[], blk:null, len:0, count:0 } )
console.log( isApsChunk(isQuestions, 3) )
.as-console-wrapper { max-height: 100% !important; top: 0 }