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

196
Views
Obtenga el int más grande del objeto dentro de la matriz que está por debajo de un límite

Actualmente estoy construyendo un bot de Discord.js donde quiero dar recompensas de roles dependiendo del nivel que alcances.

Esta es mi parte principal del código para averiguar qué rol dar, lo que funciona perfectamente cuando las recompensas de tu rol solo tienen una diferencia de 1 nivel (es decir, del 1 al 2), pero si paso del nivel 4 al 5 no lo hace. saber qué eliminar y simplemente falla porque prevLvl no está definido.

Entonces, básicamente, cuando alcanzas un determinado hito, debe otorgar un nuevo rol y eliminar el anterior.

 memberData.level = 4; levelUpRoles = [ //These ones get pushed in array by user {role: 'id1', level: 1}, {role: 'id2', level: 2}, {role: 'id3', level: 5}, {role: 'id4', level: 20}, {role: 'id5', level: 10}, ] let newLvl = levelUpRoles.find(object => object.level === memberData.level); let prevLvl = levelUpRoles.find(object => object.level === memberData.level - 1); if (newLvl) { target.roles.add(newLvl.role).catch(console.error); target.roles.remove(prevLvl.role).catch(console.error); }

Espero que esto haya tenido sentido. ¡Gracias de antemano!

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

0

Suponiendo que levelUpRoles siempre estará en orden ascendente, puede verificar el índice del valor de level más bajo por encima de memberData.level usando el operador > .

Luego, a partir de ese índice, encuentre el nuevo nivel y el nivel anterior y realice las operaciones necesarias.

 levelUpRoles = [ {role: 'id1', level: '1'}, {role: 'id2', level: '2'}, {role: 'id3', level: '5'}, ] let nextIndex = levelUpRoles.findIndex(object => object.level > memberData.level); if (nextIndex) { nextIndex > 0 || nextIndex = levelUpRoles.length; // If there is no next level const newLvl = levelUpRoles[nextIndex-1]; target.roles.add(newLvl.role).catch(console.error); if (nextIndex > 1) { // If there exists a previous level const prevLvl = levelUpRoles[nextIndex-2]; target.roles.remove(prevLvl.role).catch(console.error); } }
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!