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

175
Visualizações
Split ARRAY into chunks as they appear in sequence

I got an array of objects which I need to make into three chunks in the sequence as they are in the original array. For eg.

let arr = [{1},{2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}];

This array needs to be split like this:

let newArray = {a:[{1},{4},{7},{10},...}], b:[{2},{5},{8}], c:[{3},{6},{9}]};
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

reduce can distribute each value on its slot based on the index

As you need a,b,c rather than 0,1,2 , a small mapping is needed here mapNumLetter

let arr = ["{1}","{2}", "{3}", "{4}", "{5}", "{6}", "{7}", "{8}", "{9}", "{10}"];



// let newArray = {a:[{1},{4},{7},{10},...}], b:[{2},{5},{8}], c:[{3},{6},{9}]};

const mapNumLetter = {
  0:"a",
  1:"b",
  2:"c"
}

const newArray = arr.reduce((acc,cur,i) => {
  acc[mapNumLetter[i%3]].push(cur);
  return acc
},{a:[],b:[],c:[]})

console.log(newArray)

( as {1} , {2} ... are not valid objects, i changed them to literals "{1}" , "{2}" ... )

about 4 years ago · Juan Pablo Isaza Relatório

0

{1} is not a valid js structure

assuming than arr is an array of object you can :

  • generate a rolling char index with String.fromCharCode(current + 97)
  • use an object to have char index

let arr = [{name:1},{name:2}, {name:3}, {name:4}, {name:5}, {name:6}, {name:7}, {name:8}, {name:9}, {name:10}];

function transform(theArray, chunkValue) {
  let finalObject = {};
  let current = 0;
  let currentIndex;
  
  theArray.forEach(element => {
    currentIndex = String.fromCharCode(current + 97);
    if (!finalObject[currentIndex]) {
      finalObject[currentIndex] = [];
    }
    finalObject[currentIndex].push(element);
    current++;
    if (current === chunkValue) {
      current = 0;
    }
  });
  
  console.log(finalObject);
  return finalObject;
}

transform(arr, 3);

about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe this approach would be useful

const data = [{1:1}, {2:2}, {3:3}, {4:4}, {5:5}, {6:6}, {7:7}, {8:8}, {9:9}, {10:10}];

const chunk = (arr, n) => arr.length ? [arr.slice(0, n), ...chunk(arr.slice(n), n)] : [];

const chunks = chunk(data, 3);
const rotated = chunks[0].map((_, colIndex) => chunks.map(row => row[colIndex]));
const result = rotated.map((arr, index) => 
  ({ [index]: arr.filter(Boolean) }));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

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