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

195
Visualizações
Create custom list from main list in javascript

I have a list object like below

[{a:"0"},{a:"1"},....{a:"39"}]

I want to have a list like the one below.I need a result like the below.

{
  "list1":[{a:"0"},{a:"1"},{a:"2"},{a:"3"}],
  "list2":[{a:"4"},{a:"5"},{a:"6"},{a:"7"}],
  .
  .
  .
  "list10":[{a:"36"},{a:"37"},{a:"38"},{a:"39"}],
}

Thanks for help me.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use below code:

const items = [{a: "0"}, {a: "1"}, {a: "2"}, {a: "3"}, {a: "4"}, {a: "5"}, {a: "6"}, {a: "7"}, {a:"8"}, {a:"9"}, {a:"10"}];
let newItems = {};
let i = 1;
while(items.length){
    newItems[`list${i}`] = items.splice(0,4);
    i++;
}
console.log(newItems)

If you want too kep the items immutable you can copy it into another array before while loop, like this const copyItems = [...items] and using copyItems in while.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a reduce to achieve this

const items = [{ a: "0" }, { a: "1" }, { a: "2" }, { a: "3" }, { a: "4" }, { a: "5" }, { a: "6" }, { a: "7" }];
items.reduce((accum, curr, index) => {
  const key = `list${1 + Math.floor(index / 4)}`;
  accum[key] = accum[key] || [];
  accum[key].push(curr);
  return accum;
}, {});
about 4 years ago · Juan Pablo Isaza Relatório

0

I believe this might be what you're looking for. This will split the big array into smaller ones and store each array with a size of 4 in the arrayOfArrays.

var bigarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var size = 4; var arrayOfArrays = [];
for (var i=0; i<bigarray.length; i+=size) {
  arrayOfArrays.push(bigarray.slice(i,i+size));
}
console.log(arrayOfArrays); // [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
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