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

165
Vistas
How to split the array if the Array.length is higher than 5 in javascript
const array = [1...7]

so i have this array as u can see the length is 7, so i want the result like this

[1, 2, 3, 4, 5] and [6, 7]


same for the array below

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

the length is 11, therefore the result i want should be

[1, 2, 3, 4, 5] and [7, 8, 9, 10] and [11]

why 11 only? because i want to seperate array lengths by 5

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I had written an splitter function that does the job:

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

function splitter ( arr, length = 5 ) {
  let result = [];
  let intermediateArray = [];
  arr.reverse().map( (el,index) => {
    intermediateArray.push(el);
   
    if (index % length === 0) {
      result.push(intermediateArray.reverse());
      intermediateArray = []
    }
  })
  return result.reverse();
}

console.log(splitter(array))

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