Anders
Anders

Reputation: 385

Find a value in a multidimensional array if I know another?

I have a multidimensional array and uses jQuery. I know shortname, how do I get the conversionSI and put the result in a variable, console.log or something else? Or could my array look in a better way (as I want it to be as quick as possible).

My Array

var UnitArray = [ {
 name : "liter",
 shortName : "l",
 unitType : "volym",
 country : "sv",
 conversionSI : 1
 }, {
 name : "deciliter",
 shortName : "dl",
 unitType : "volym",
 country : "sv",
 conversionSI : 0.1
 }, {
 name : "centiliter",
 shortName : "cl",
 unitType : "volym",
 country : "sv",
 conversionSI : 0.01
 }];

E.g I know the shortnames "cl" and "dl" and want to use the conversionSI from them in a calculation.

Upvotes: 3

Views: 98

Answers (6)

Aleksey Lysenko
Aleksey Lysenko

Reputation: 21

I guess you have an array of short names and want to get an array of corresponding values (conversionSI in your case). You can look at:

var UnitArray = []; // here your array is
var shortnames = ["cl", "dl"];
var conversionSIs = UnitArray
    .reduce(function (foundValues, unitElem) {
        if (~shortnames.indexOf(unitElem.shortName)) {
          foundValues.push(unitElem.conversionSI);
        }
        return foundValues;
    }, []);

console.log('conversionSIs', conversionSIs);

Upvotes: 0

Nenad Vracar
Nenad Vracar

Reputation: 122057

You can create a function like this DEMO

var getConversion = function(el) {
  for (var i = 0; i < UnitArray.length; i++) {
    var obj = UnitArray[i];
    if (obj.shortName == el) return obj.conversionSI;
  }
}

console.log(getConversion('cl'));

Updated function that returns array with results Demo

Upvotes: 2

Joe Withey
Joe Withey

Reputation: 991

We can create a function that will return the index of an item based on a desired property and value. Note that it will only return the first instance. The reduce method is ES5 so it is generally well supported now.

var UnitArray = [ {
  name : "liter",
  shortName : "l",
  unitType : "volym",
  country : "sv",
  conversionSI : 1
}, {
  name : "deciliter",
  shortName : "dl",
  unitType : "volym",
  country : "sv",
  conversionSI : 0.1
}, {
  name : "centiliter",
  shortName : "cl",
  unitType : "volym",
  country : "sv",
  conversionSI : 0.01
}];

function findIndexByProp(prop, value, arr) {
  return arr.reduce(function(a, c, i) {
    if (a !== -1) return a;

    return (c[prop] === value) ? i : a;
  }, -1);
}

var index = findIndexByProp('shortName', 'dl', UnitArray);

var conversionSI = UnitArray[index].conversionSI;

Upvotes: 0

Andy
Andy

Reputation: 63524

Here's a general function that will get you the value of any property based on a supplied property/value to filter on.

function getVal(arr, filterProp, filterVal, prop) {
  return arr.filter(function (el) {
    return el[filterProp] === filterVal;
  }).map(function (el) {
    el[prop];
  });
}

getVal(UnitArray, 'shortName', 'cl', 'conversionSI') // [ 0.01, 0.01 ]

DEMO

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

A solution with short circuit if found

function getValue(array, shortName, property) {
    var r;
    array.some(function (a) {
        if (a.shortName === shortName) {
            r = a[property];
            return true;
        }
    });
    return r;
}

var UnitArray = [{ name: "liter", shortName: "l", unitType: "volym", country: "sv", conversionSI: 1 }, { name: "deciliter", shortName: "dl", unitType: "volym", country: "sv", conversionSI: 0.1 }, { name: "centiliter", shortName: "cl", unitType: "volym", country: "sv", conversionSI: 0.01 }];

document.write(getValue(UnitArray, 'cl', 'conversionSI') + '<br>');
document.write(getValue(UnitArray, 'dl', 'conversionSI') + '<br>');

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

You can use .find() at this context,

console.log(UnitArray.find(v => v.shortName == "cl").conversionSI); // 0.01

You can make it as a function so that it would be handy,

var getConversionSI = 
     shortName => UnitArray.find(v => v.shortName == shortName ).conversionSI
console.log(getConversionSI("cl")); // 0.01

Upvotes: 0

Related Questions