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

142
Views
Ordenar función con condición múltiple

Tengo una matriz de objetos como a continuación:

 var data = [ { name": "Name2", "webOrderingEnabled": true, "distance": 1.6989125091571928 }, { "name": "Name3", "webOrderingEnabled": false, "distance": 1.9178283920396098 }, { "name": "Name4", "webOrderingEnabled": false, "shutdown": { "message": "", "status": true }, "distance": 6.94478210395609 }, { "name": "Name1", "webOrderingEnabled": true, "shutdown": { "message": "", "status": false }, "distance": 0.5368834377514055 } ]

Quiero ordenar esta matriz de objetos 1.webOrderingEnabled 2.shutdown.status = false 3. Distancia El desafío es que algún objeto no tiene la tecla de apagado si no está presente, considere que está abierto en la tienda. Intenté el siguiente enfoque, no funcionó. para mi

 data.sort((a, b) => { if (a.shutdown?.status && b.shutdown?.starus || !a.shutdown?.status && !b.shutdown?.status && a.webOrderingEnabled || b.webOrderingEnabled) { return a.distance - b.distance; } if (a.shutdown?.status) { return -1; } return 1; });

Esperado:

 [{ "name": "Name1", "webOrderingEnabled": true, "shutdown": { "message": "", "status": false }, "distance": 0.5368834377514055 }, { "name": "Name2", "webOrderingEnabled": true "distance": 1.6989125091571928 }, { "name": "Name3", "webOrderingEnabled": false, "distance": 1.9178283920396098 }, { "name": "Name4", "webOrderingEnabled": false, "shutdown": { "message": "", "status": true }, "distance": 6.94478210395609 }]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En general, si desea ordenar una matriz de objetos de acuerdo con los campos A, B, C (en este orden), simplemente haga lo siguiente:

 if (aA < bA) return -1; else if (aA > bA) return 1; // otherwise aA == bA, so let's proceed with considering B now... if (aB < bB) return -1; else if (aB > bB) return 1; // otherwise aA == bA && aB == bB, so let's proceed with considering C now... if (aC < bC) return -1; else if (aC > bC) return 1; return 0;

Aquí puede pensar en aA < bA como un pseudocódigo para comparar ese valor de atributo único, exactamente cómo se hace eso depende de los tipos.

Así que tu código debería verse algo como

 // I assume by "consider to be store open" you mean shutdown.status === false function getShutdownStatus(a) { return a?.shutdown?.status || false; } data.sort((a, b) => { // Use ! here because true > false but it seems you want true to come first if (!a.webOrderingEnabled < !b.webOrderingEnabled) return -1; else if (!a.webOrderingEnabled > !b.webOrderingEnabled) return 1; const aShutdown = getShutdownStatus(a); const bShutdown = getShutdownStatus(b); if (aShutdown < bShutdown) return -1; else if (aShutdown > bShutdown) return 1; // otherwise compare by distance if (a.distance < b.distance) return -1; else if (a.distance > b.distance) return 1 return 0; });

Una vez que tenga esto funcionando, puede simplificar el código:

 data.sort((a, b) => { if (a.webOrderingEnabled != b.webOrderingEnabled) return b.webOrderingEnabled - a.webOrderingEnabled; const aShutdown = getShutdownStatus(a); const bShutdown = getShutdownStatus(b); if (aShutdown != bShutdown) return aShutdown - bShutdown; return a.distance - b.distance; });
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!