I have a multidimensional array as follows:
JavaScript
x
5
1
var schMatrix = [
2
['Weight',100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000],
3
['Rate',395,413,484,560,635,691,763,837,910,982,1082,1155,1231,1304,1379,1452,1527,1570,1676,1750],
4
];
5
In my form the user would input a weight. What I am trying to do is if the weight is equal to or below the number in the weight part of the array that the corresponding rate is assigned to a variable.
Am stuck on how to do this. Any push in the right direction is appreciated.
Advertisement
Answer
First of all i would suggest you use a Object instead of a 2d-Array:
JavaScript
1
5
1
let schMatrix = {
2
"Weight": [ , , ],
3
"Rate": [ , , ]
4
}
5
Then you can itterate through the “Weight”-Array:
JavaScript
1
6
1
for (let i = 0; i < schMatrix["Weight"].length; i++) {
2
if(schMatrix["Weight"][i] >= inputWeight){
3
rate = schMatrix["Rate"][i]
4
}
5
}
6