Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda