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

243
Views
Obtener un valor de una matriz multidimensional

Tengo una matriz multidimensional de la siguiente manera:

 var schMatrix = [ ['Weight',100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000], ['Rate',395,413,484,560,635,691,763,837,910,982,1082,1155,1231,1304,1379,1452,1527,1570,1676,1750], ];

En mi formulario, el usuario ingresaría un peso. Lo que estoy tratando de hacer es que si el peso es igual o inferior al número en la parte de peso de la matriz, la tasa correspondiente se asigna a una variable.

Estoy atascado en cómo hacer esto. Se agradece cualquier empujón en la dirección correcta.

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

0

En primer lugar, le sugiero que use un objeto en lugar de una matriz 2d:

 let schMatrix = { "Weight": [...,...,...], "Rate": [...,...,...] }

Luego puede iterar a través de la matriz "Peso":

 for (let i = 0; i < schMatrix["Weight"].length; i++) { if(schMatrix["Weight"][i] >= inputWeight){ rate = schMatrix["Rate"][i] } }
about 4 years ago · Juan Pablo Isaza Report

0

Estarás mejor usando un mapa. Son mucho más rápidos y puede asignar cada peso a una tasa específica:

 MAP = { 100: 395, 200: 413, 300: 484, etc... } map.get(100) //outputs 395

Para asignar las matrices a un mapa, haga lo siguiente:

 let map = new Map(); for(let i = 0; i < arr1.length; i++){ map.set(arr1[i], arr2[i]); }
about 4 years ago · Juan Pablo Isaza Report

0

var schMatrix = [ ['Weight', 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000], ['Rate', 395, 413, 484, 560, 635, 691, 763, 837, 910, 982, 1082, 1155, 1231, 1304, 1379, 1452, 1527, 1570, 1676, 1750], ] function getRate(weight) { for (let i = 1; i < schMatrix[0].length; i++) { const currWeight = schMatrix[0][i] if (weight > currWeight) { // as soon as currWeight is bigger than weight, return return schMatrix[1][i-1] } } }
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!