Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Encontrar el índice condicional de un elemento en una lista de objetos

Necesito encontrar el índice de un elemento de una lista de objetos. Primero tengo que verificar si ese elemento existe con el estado de ESPERANDO . Si no existe ningún artículo con ese estado, busque otro con cualquier otro estado. ¿Hay alguna solución mejor para esto?

x en este código proveniente de un mapa

 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 answers
Answer question

0

No habrá una "solución de una sola línea" razonable (esas están sobrevaloradas en cualquier caso; difíciles de leer, difíciles de depurar); pero puede evitar buscar dos veces en la matriz utilizando un bucle for :

 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; });

O si realmente desea usar findIndex , puede evitar algunas búsquedas al encontrar primero el primero con una slot coincidente:

 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; });

Tenga en cuenta que los dos anteriores devuelven -1 si orders son falsas, en lugar de undefined . Tweak si realmente quería undefined .

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar findIndex para buscar un elemento con un estado igual a 'ESPERANDO'. en caso de que no exista ningún elemento, use findIndex para devolver el primer elemento con un estado.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!