I have a problem about chunking subarrays. As you see below, there are many òbject inside detail. I want to split them if detail[i].length + detail[i+1].length >= 11 chunk array with 2, else chunk 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} ] }
]
Expected array be like;
[ [ { 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} ] }
] ]
As you see above, the sum of expected array's last part is 12. But It does not matter, the important thing is arr[i] + arr[i+1] length.
I did a function like inside map function. Because I have multiple array like this.
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))
But the problem is that function takes array with the length 7, gives me 5.