Q:

Write a JavaScript program to create an array of elements, grouped based on the position in the original arrays

0

Write a JavaScript program to create an array of elements, grouped based on the position in the original arrays.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const zip = (...arrays) => {
  const maxLength = Math.max(...arrays.map(x => x.length));
  return Array.from({ length: maxLength }).map((_, i) => {
    return Array.from({ length: arrays.length }, (_, k) => arrays[k][i]);
  });
};

console.log(zip(['a', 'b'], [1, 2], [true, false]));
console.log(zip(['a'], [1, 2], [true, false]));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now