Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
chunk array into multiple arrays

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"];

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!