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

250
Views
convertir while loop a .forEach tiene un resultado inesperado

En un ejemplo útil de cómo ordenar una matriz de objetos usando múltiples propiedades, el código que realiza la ordenación usa un bucle while (ver students1 en el fragmento de código a continuación). Estoy tratando de simplificar el ciclo while usando .forEach pero obtengo un conjunto de resultados diferente (ver students2 ). ¿Dónde salieron mal las cosas en la refactorización usando .forEach que no da el mismo resultado que el ciclo while ?

 const students = [ { firstName: 'John', lastName: 'Appletree', grade: 12 }, { firstName: 'Mighty', lastName: 'Peachtree', grade: 10 }, { firstName: 'Kim', lastName: 'Appletree', grade: 11 }, { firstName: 'Shooter', lastName: 'Appletree', grade: 12 }, { firstName: 'Peter', lastName: 'Peachtree', grade: 12 } ]; const sortBy = [ { prop:'grade', direction: -1 }, { prop:'lastName', direction: 1 } ]; const students1 = JSON.parse(JSON.stringify(students)); const students2 = JSON.parse(JSON.stringify(students)); students1.sort((a, b) => { let i = 0, result = 0; while (i < sortBy.length && result === 0) { const prop = sortBy[i].prop; const direction = sortBy[i].direction; result = direction * ( a[prop].toString() < b[prop].toString() ? -1 : (a[prop].toString() > b[prop].toString() ? 1 : 0) ); i++; } return result; }); students2.sort((a, b) => { let result = 0; sortBy.forEach(o => { result = o.direction * ( a[o.prop].toString() < b[o.prop].toString() ? -1 : (a[o.prop].toString() > b[o.prop].toString() ? 1 : 0) ); }); return result; }); console.log(students); console.log('one', students1); console.log('two', students2);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Tus bucles forEach y while no son realmente equivalentes. Si la condición while fuera simplemente i < sortBy.length , entonces funcionaría. Un poco aquí tiene una condición adicional de result === 0 , que forEach no tiene en cuenta.

about 4 years ago · Juan Pablo Isaza Report

0

No ha implementado completamente la condición del bucle while :

 const students = [ { firstName: 'John', lastName: 'Appletree', grade: 12 }, { firstName: 'Mighty', lastName: 'Peachtree', grade: 10 }, { firstName: 'Kim', lastName: 'Appletree', grade: 11 }, { firstName: 'Shooter', lastName: 'Appletree', grade: 12 }, { firstName: 'Peter', lastName: 'Peachtree', grade: 12 } ]; const sortBy = [ { prop:'grade', direction: -1 }, { prop:'lastName', direction: 1 } ]; const students1 = JSON.parse(JSON.stringify(students)); const students2 = JSON.parse(JSON.stringify(students)); students1.sort((a, b) => { let i = 0, result = 0; while (i < sortBy.length && result === 0) { const prop = sortBy[i].prop; const direction = sortBy[i].direction; result = direction * ( a[prop].toString() < b[prop].toString() ? -1 : (a[prop].toString() > b[prop].toString() ? 1 : 0) ); i++; } return result; }); students2.sort((a, b) => { let result = 0; sortBy.forEach(o => { if (result !== 0) return; result = o.direction * ( a[o.prop].toString() < b[o.prop].toString() ? -1 : (a[o.prop].toString() > b[o.prop].toString() ? 1 : 0) ); }); return result; }); console.log(students); console.log('one', students1); console.log('two', students2);

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!