简体   繁体   English

如果我知道另一个,在多维数组中找到一个值?

[英]Find a value in a multidimensional array if I know another?

I have a multidimensional array and uses jQuery. 我有一个多维数组并使用jQuery。 I know shortname , how do I get the conversionSI and put the result in a variable, console.log or something else? 我知道短名称 ,我如何获得conversionSI并将结果放在变量,console.log或其他东西? 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
 }];

Eg I know the shortnames "cl" and "dl" and want to use the conversionSI from them in a calculation. 例如,我知道短名称“cl”和“dl”,并希望在计算中使用它们的conversionSI。

You can create a function like this DEMO 您可以创建一个像这个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 更新的函数返回带有结果Demo的数组

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>'); 

You can use .find() at this context, 您可以在此上下文中使用.find()

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

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 DEMO

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. reduce方法是ES5,因此现在通常得到很好的支持。

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;

I guess you have an array of short names and want to get an array of corresponding values (conversionSI in your case). 我想你有一个短名称数组,想要得到一个相应值的数组(在你的情况下是conversionSI)。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM