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

169
Visualizações
Remove items from an array in order until matching item is found

I have an array of items

const test = [
  {
    id: '1',
  },
  {
    id: 'a',
  },
  {
    id: '3',
  },
  {
    id: 'b',
  },
  {
    id: '5',
  },
  {
    id: 'c',
  },
  {
    id: '7',
  },
];

These objects are always going to be in the same order. I'd like to be able to remove all items in order from the start until a specific id is reached.

I thought I could do this by just looping through until one is found and then pushing the items coming after that to a separate array:

let skippedArray = []

let skipUntilIndex = undefined

test.forEach((item, i) => {
  if(item.id === 'b') {
    skipUntilIndex = i
  }
  if(skipUntilIndex && i >= skipUntilIndex) {
    skippedArray.push(item)
  }
})

// [
//   {
//     id: 'b',
//   },
//   {
//     id: '5',
//   },
//   {
//     id: 'c',
//   },
//   {
//     id: '7',
//   },
// ]
console.log(skippedArray)

But this seems pretty dirty. Is there a better way to achieve this? I don't mind using lodash or similar libraries.

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

0

You can do it by using findIndex and slice methods, by findIndex you can find the index of target item in the array and with slice you can make a subArray of your original Array.

const data = [ { id: '1', }, { id: 'a', }, { id: '3', }, { id: 'b', }, { id: '5', }, { id: 'c', }, { id: '7', }, ];

const getSkippedItems = (id)=> {
    const index = data.findIndex(v => v.id == id);
    return index > -1 ? data.slice(index) : [];
}

const skippedArr = getSkippedItems('b');
console.log(skippedArr)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.reduce() to filter out all items until our desired id is reached.

We'll only create the output array once we find the required id, then push remaining items into it.

const test = [ { id: '1' }, { id: 'a' }, { id: '3' }, { id: 'b' }, { id: '5' }, { id: 'c' }, { id: '7' } ];
 
const result = test.reduce((output, item) => { 
    if (!output && item.id === 'b') output = [];
    if (output) output.push(item);
    return output;
}, null);

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

about 4 years ago · Juan Pablo Isaza Relatório

0

You could take a closure over a flag and if found filter the rest by found.

const
    removeUntil = (id, found) => o => found ||= o.id === id,
    test = [{ id: '1' }, { id: 'a' }, { id: '3' }, { id: 'b' }, { id: '5' }, { id: 'c' }, { id: '7' }],
    result = test.filter(removeUntil('b'));

console.log(result);
.as-console-wrapper { max-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