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

228
Visualizações
Finding conditional index of an item in a list of objects

I need to find index of an item from a list of objects. First I have to check if that item exist with status of WAITING. If no item with that status exist, then find something else with any other status. Is there any better solution for this?

x in this code coming from a map

MainArray.map((x) => {
    let itemIndex = orders?.findIndex(item => item.status === 'WAITING' && item.slot=== (x));
    if (itemIndex === -1) {
        itemIndex = orders && orders.findIndex(item => item.slot === (x));
    }
    return itemIndex;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There won't be a reasonble "single line solution" (those are over-rated in any case; hard to read, hard to debug); but you can avoid searching through the array twice by using a for loop:

const indexes = MainArray.map((x) => {
    let bySlotIndex;
    for (let index = 0, length = orders?.length; orders && index < length; ++index) {
        const order = orders[index];
        if (item.slot === x) {
            bySlotIndex = bySlotIndex ?? index;
            if (item.status === "WAITING") {
                return index; // Found a waiting one, we're done
            }
        }
    }
    return bySlotIndex ?? -1;
});

Or if you really want to use findIndex, you can avoid some searching by finding the first one with a matching slot first:

const indexes = MainArray.map((x) => {
    const bySlotIndex = orders?.findIndex(order => order.slot === x) ?? -1;
    if (bySlotIndex === -1) {
        return -1;
    }
    const waitingIndex = orders.findIndex(
        order => order.status === 'WAITING' && order.slot === x,
        bySlotIndex // Start at the first one with a matching slot
    ); 
    return waitingIndex === -1 ? bySlotIndex : waitingIndex;
});

Note that both of the above return -1 if orders is falsy, rather than undefined. Tweak if you really wanted undefined.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use findIndex to look for an item with status equal to 'WAITING'. in case, no item exists, use findIndex to return the first item with a status.

const arr = [{status: "WAITING", slot : 1}, {status: "NOTWAITING", slot: 2}];

const getIndex = (arr) => {
   const idx = arr.findIndex(x => x.status === 'WAITING');
   return idx !== -1 ? idx : arr.findIndex(x => x.status);
}

console.log(getIndex(arr));

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