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

176
Visualizações
Pushing x number of array items to another array conditionally

I want to push x number of array items to another array with the condition that the maximum number of items that will be pushed be the difference of maxPushLimit (which is 720) and alreadyPushed (so if alreadyPushed is, say, 300, only 420 can be pushed)

Special conditions:

  • If the total of all alreadyPushed from array1 is > total length of array2, ignore the last difference (last x items left from array2.
  • If the total of all alreadyPushed from array1 is < total length of array2, last index of array1 would have redundance (wouldn't be 720, say, 710 if the difference is 10).

I wrote this code to achieve it:
const array1 = [
  {
    id: 1,
    alreadyPushed: 500,
  },
  {
    id: 2,
    alreadyPushed: 600,
  },
  {
    id: 3,
    alreadyPushed: 700,
  },
  {
    id: 4,
    alreadyPushed: 720,
  },
];

const array2 = [];
for (let i = 1; i < 361; i++) {
  array2.push(i);
}

const maxPushLimit = 720;

let results = [];
let array2Index = 0;

for (let i = 0; i < array1.length; i++) {
  const array1Item = array1[i];
  const limit = maxPushLimit - array1Item.alreadyPushed;
  console.log(limit);

  if (limit <= 0) {
    continue;
  } else {
    array1Item.array2Items = array2.slice(array2Index, limit);
    array2Index += limit;
    results.push(array1Item);
  }
}

console.log(results);

Keep in mind that the order of alreadyPushed is not important (500, 600, 700, 720 are totally random)
I want the end result to look like this:

[
    {
      id: 1,
      alreadyPushed: 500,
      array2Items: {
          // 220 array2Items
      }
    },
    {
      id: 2,
      alreadyPushed: 600,
      array2Items: {
          // 120 array2Items
      }
    },
    {
      id: 3,
      alreadyPushed: 700,
      array2Items: {
          // 20 array2Items
      }
    },
];

What I'm getting:

[
  {
    id: 1,
    alreadyPushed: 500,
    array2Items: [
       // 220 array2Items
    ]
  },
  { id: 2, alreadyPushed: 600, array2Items: [] },
  { id: 3, alreadyPushed: 700, array2Items: [] }
]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is the slice operation. It should be array2.slice(array2Index, array2Index + limit);.

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is in this expression:

array2.slice(array2Index, limit);

The second argument should not be the size of the slice, but the ending index of the slice.

So change to:

array2.slice(array2Index, array2Index + limit);
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