Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda