penone
penone

Reputation: 792

Get a value from a multidimensional array

I have a multidimensional array as follows:

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],
    ];

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.

Upvotes: 2

Views: 74

Answers (5)

twharmon
twharmon

Reputation: 4272

This works, if you assume the weights are sorted already.

function getRate(matrix, target) {
  const [weights, rates] = matrix;
  for (let i = 1; i < weights.length; i++) {
    if (weights[i] > target) {
      return rates[i];
    }
  }
}

var schMatrix = [
  ['Weight',100,200],
  ['Rate',395,413],
];

const rate = getRate(schMatrix, 110);
console.log(rate); // 413

Upvotes: 0

sno2
sno2

Reputation: 4173

You can do this by finding the corresponding weights and rates columns via the Array.prototype.find method. After that, check if the index plus one (because the label is the first item in the indexing) in "Weight" is less than or equal to the corresponding item in "Rate". Here's the code:

const 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 weightLessThanEqRate(i) {
    const weights = schMatrix.find((item) => item[0] === "Weight");
    const rates = schMatrix.find((item) => item[0] === "Rate");
    return weights[i + 1] <= rates[i + 1];
}

console.log(weightLessThanEqRate(5));

TypeScript Playground Link

Upvotes: 0

Aaron Vasilev
Aaron Vasilev

Reputation: 110

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]
     }
   }
 }

Upvotes: 0

Sean
Sean

Reputation: 1456

You will be better off using a map. They are much faster and you can map each weight to a specific rate:

MAP = {
  100: 395,
  200: 413,
  300: 484,
  etc...
}

map.get(100) //outputs 395

To map the arrays to a map do the following:

let map = new Map();
for(let i = 0; i < arr1.length; i++){
   map.set(arr1[i], arr2[i]);
}

Upvotes: 0

Timm Nicolaizik
Timm Nicolaizik

Reputation: 411

First of all i would suggest you use a Object instead of a 2d-Array:

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

Then you can itterate through the "Weight"-Array:

for (let i = 0; i < schMatrix["Weight"].length; i++) {
    if(schMatrix["Weight"][i] >= inputWeight){
        rate = schMatrix["Rate"][i]
    }
}

Upvotes: 1

Related Questions