Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
How to split array of arrays into smaller parts

I have something like this, an array of arrays where each array has a 7 values

const arrays = [[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]]

What's the most efficient way to split this into array of arrays where each array will have a 2 values and last array of arrays will hold the remaining 1 element, like this

[[1,2], [8,9]] [[3,4], [10,11]] [[5,6], [12,13]] [[7], [14]]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use array#reduce and array#forEach to split your array based on size.

const arr = [[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]],
      size = 2,
      result = arr.reduce((r, a, i) => {
        a.forEach((v,j) => {
          const idx = Math.floor(j/size);
          r[idx] = r[idx] || [];
          r[idx][i] = r[idx][i] || [];
          r[idx][i].push(v);
        });
        return r;
      },[]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

It doesn't look that efficient, but it works.

const arrays = [[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]];

// number of sub arrays
var numsubarr = Math.ceil(arrays[0].length / 2);

// parring up the vaules
var arr1 = arrays.map(arr => Array.from(Array(numsubarr).keys()).map(i => arr.slice(i*2, i*2+2)));

// combining the two arrays
var arr2 = Array.from(Array(numsubarr).keys()).map(i => [arr1[0][i],arr1[1][i]]);

console.log(arr2);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda