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

193
Visualizações
How to filter an array of numbers and take certain intervals?

I have this type of array.

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

Requirements to filter the array are

  1. Omit every 4 items
  2. Take the following 4 items

And continue until the end of the array.

At the end It should return [5,6,7,8,13,14,15,16,21,22,23,24]

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

const values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
newValues = values.filter((v, i) => i % 8 > 3)
console.log(newValues)

about 4 years ago · Santiago Trujillo Relatório

0

You could slice the chunks and add to the result array.

const
    data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
    result = [];
    
let i = 0;
while (i < data.length) result.push(...data.slice(i += 4, i += 4));

console.log(...result);

about 4 years ago · Santiago Trujillo Relatório

0

You can create a partitioning function for achieving your result.

const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];

/**
 * Pertition the array and create a new array.
 *
 * @param {array}  arr The array to pertition.
 * @param {number} skip The number of elements to skip
 * @param {number} take Take the number of elements need to take after skipping.
 *
 * @return {array} The resulting array after pertition.
*/
const arrayAfterPertition = (arr, skip, take) => {
  const length = arr.length;
  let ans = [];
  
  /** If there are not enough elements to skip then returns empty array */
  if (length <= skip) return [];
  
  let takeIndex = skip;
  
  while (takeIndex < length) {
    ans = [...ans, ...arr.slice(takeIndex, takeIndex + take)];
    takeIndex = takeIndex + take + skip;
  }
  
  return ans;
}

console.log(JSON.stringify(arrayAfterPertition(arr, 4, 4)));
.as-console-wrapper {min-height: 100%!important; top: 0}

about 4 years ago · Santiago Trujillo 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