1

I'm trying to chunk my array into 3 using this code

var a = ["a", "b", "c", "d", "e", "f", "g"];
let chunk;

while (a.length > 0) {
  chunk = a.splice(0, 3);
  console.log(chunk);
}

but how can I get a result something like these

var array1 = ["a", "d", "g"];
var array2 = ["b", "e"];
var array3 = ["c", "f"];

jabaa
  • 5,844
  • 3
  • 9
  • 30
johnv
  • 37
  • 3
  • the first chunk must always have three items? I doesnt matter if the array have 10 , 20 or more items? – Maik Lowrey Apr 02 '22 at 18:48
  • @jabaa sorry for the confusion. Yes, I want to split it into 3 arrays, but the result must not be something like this one array1 = ["a", "b", "c", ...], please see above for the sample result. thank you – johnv Apr 02 '22 at 19:04
  • 1
    @MaikLowrey yes, it doesn't matter if the array has more than 3 or more elements, but the pattern or result must be something in the sample above. thank you – johnv Apr 02 '22 at 19:06
  • Iterate over `a` and put the first element into `array1`, the second element into `array2`, the third element into `array3`, ... The [remainder operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder) could be helpful. – jabaa Apr 02 '22 at 19:09

2 Answers2

2

const a = ["a", "b", "c", "d", "e", "f", "g"];
const numberOfArrays = 3;
let arrays = Array.apply(null, Array(numberOfArrays)).map(it=>[])
a.forEach((value,i)=>{
  arrays[(i%numberOfArrays)].push(value);
})
console.log(arrays)
Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
  • Can you explain what `.map(it=>[])` do, please? – Maik Lowrey Apr 02 '22 at 19:52
  • 1
    Array(numberOfArrays) create array with 3 values map(it=>[]) replace every element with empty array there is a great explanation i recommend you the read here https://stackoverflow.com/a/28599347/4267015 – Naor Tedgi Apr 02 '22 at 19:56
  • Thank you for answering. I tried with `Array(3).fill([]);` and got same `[[],[],[]]`. Afterwards i run your foreach: `a.forEach((value,i)=>{ arrays[(i%3)].push(value); })` But i got completly wrong result. I dont understand it. – Maik Lowrey Apr 02 '22 at 20:01
  • `a = ["a", "b", "c", "d", "e", "f", "g"]; chunk = Array(3).fill([]); a.forEach((value,i) => { chunk[(i%3)].push(value); }) console.log(chunk) ` full code for testing – Maik Lowrey Apr 02 '22 at 20:02
  • 1
    from msdn`Value to fill the array with. (Note all elements in the array will be this exact value.)` arrays and object are copied by ref in js all the elements in chunks as same ref to the array you pass to fill function, with map each cluser create new array – Naor Tedgi Apr 02 '22 at 20:05
  • Okay. Thank you for explanation. To be honest, I haven't quite understood it yet, but I can guess what it means. ;-) Merci – Maik Lowrey Apr 02 '22 at 20:09
  • what means `msdn Value`? – Maik Lowrey Apr 02 '22 at 20:11
  • run this example then everything make more sense a = ["a", "b", "c", "d", "e", "f", "g"]; const arr = ["moshe"] chunk = Array(3).fill(arr); a.forEach((value, i) => { chunk[(i % 3)].push(value); }) arr.push("x") console.log(chunk) – Naor Tedgi Apr 02 '22 at 20:12
  • 1
    msdn https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill – Naor Tedgi Apr 02 '22 at 20:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243548/discussion-between-maik-lowrey-and-naor-tedgi). – Maik Lowrey Apr 02 '22 at 20:17
0

Iterate over a and put the first element into array1, the second element into array2, the third element into array3, ...

const a = ["a", "b", "c", "d", "e", "f", "g"];

const [array1, array2, array3] = a.reduce((acc, el, idx) => {
  acc[idx % 3].push(el);
  return acc;
}, [[], [], []]);

console.log(array1);
console.log(array2);
console.log(array3);
jabaa
  • 5,844
  • 3
  • 9
  • 30