javascript 使用 js 在列表列表中查找值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11173907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:16:20  来源:igfitidea点击:

Lookup value in a list of lists, using js

javascriptflot

提问by David__

I have a list of data in javascript that looks like this:

我在 javascript 中有一个数据列表,如下所示:

[[152, 48, 'http://www.google.com'], 
 [198, 47, 'http://www.stackoverflow.com'], 
 [199, 45, 'http://www.apple.com']]

I am using flot to create a plot, and am trying to pass this third value to access a hyperlink from the point. As such, I am trying to lookup the third value of each list by using the first two as the lookup keys (i.e., [[x,y,hyperlink],[x2,y2,hyperlink2]], click on a point, then use the appropriate (x,y)to find the corresponding hyperlink)

我正在使用 flot 创建一个图,并试图传递这第三个值以从该点访问超链接。因此,我试图通过使用前两个作为查找键来查找每个列表的第三个值(即[[x,y,hyperlink],[x2,y2,hyperlink2]],单击一个点,然后使用适当(x,y)的查找相应的超链接)

Is there anyway to do this, or do I need to pass some dictionaries for x and y to javascript, then find the common variable from the two lists that were looked up? In python I know you could do a filter of list on the xvalue with itemgetter, then lookup a link corresponding to the yvalue. But I know almost nothing about js, so could a solution to ID-ing with (x,y)be given, or if not possible or advised, then a solution to taking two lists of (from x and y vals) and find a common value (if multiple, just one, anyone)?

无论如何要这样做,还是我需要将一些 x 和 y 的字典传递给 javascript,然后从查找的两个列表中找到公共变量?在python中,我知道您可以x使用itemgetter对值进行列表过滤,然后查找与该y值对应的链接。但是我对 js 几乎一无所知,所以可以给出一个 ID-ing 的解决方案(x,y),或者如果不可能或建议,那么一个解决方案来获取两个列表(来自 x 和 y vals)并找到一个公共值(如果有多个,只有一个,任何人)?

采纳答案by Graham.Fraser

somthing like this maybe

也许像这样的东西

  var findX = 198
  var findY = 47
  var targetUrl
  for (var i=0; i<arr.length; i++)
  {
      for (var j=0; j<arr[i].length; j++)
      {
          if (findX = j[0] && findY == j[1])
          {
              targetUrl = j[2]
          } 
      }
  }

回答by nnnnnn

You can make use of the Array .filter()methodto figure out if any elements match the supplied x and y. (Note that IE didn't support .filter()until version 9, but MDN has a shimthat you can include).

您可以使用Array.filter()方法来确定是否有任何元素与提供的 x 和 y 匹配。(请注意,IE.filter()直到版本 9才支持,但 MDN 有一个可以包含的垫片)。

var data = [[152, 48, 'http://www.google.com'],
            [198, 47, 'http://www.stackoverflow.com'],
            [199, 45, 'http://www.apple.com']];

function getURLForPoint1(x, y) {
    var p = data.filter(function(el) {
        return (el[0] === x && el[1] === y);
    });
    if (p.length === 1) return p[0][2];
    // else 0 or more than 1 elements mathced so return undefined
}

Alternatively you can create a dictionary object up front and then do future lookups from the dictionary:

或者,您可以预先创建一个字典对象,然后从字典中进行将来的查找:

var getURLForPoint2 = function() {
    var dataDictionary = {}, i;
    for (i = 0; i < data.length; i++)
       dataDictionary[data[i][0]+" "+data[i][1]] = data[i][2];

    return function(x, y) {
       return dataDictionary[x + " " + y];
    };
}();

Either way I've coded it so that if you ask for a point that isn't in the list you'll get undefinedback, but obviously you can change that to return an empty string or throw an exception or whatever you like.

无论哪种方式,我都对其进行了编码,以便如果您要求不在列表中的点,您将undefined返回,但显然您可以将其更改为返回空字符串或抛出异常或任何您喜欢的。

alert(getURLForPoint1(198, 47));    // 'http://www.stackoverflow.com'
alert(getURLForPoint2(198, 47));    // 'http://www.stackoverflow.com'
alert(getURLForPoint2(4, 5));       // undefined

Demo of both: http://jsfiddle.net/WdSAz/?

两者的演示:http: //jsfiddle.net/WdSAz/

回答by Crayon Violent

Sorry, no shortcut way to do it in js except to just loop through the list and find the one that has the matching "x" and "y" value.

抱歉,除了循环遍历列表并找到具有匹配“x”和“y”值的那个之外,没有在 js 中执行此操作的捷径。

However, depending on how large your list is (and whether or not this list will be used for something else...) you could restructure the data to make it more efficient. For instance, do a structure like (assumed possible to have for instance x1, y1 vs x1, y2)

但是,根据您的列表有多大(以及此列表是否将用于其他用途...),您可以重组数据以使其更高效。例如,做一个类似的结构(假设可能有例如 x1, y1 vs x1, y2)

x1 > y1 > url
x1 > y2 > url
x2 > y1 > url
etc...

then you can immediately jump to the 2nd lvl "y" list by the "x" index, and the only looping would be how many "y" values share the same "x" value

然后您可以立即通过“x”索引跳转到第二个lvl“y”列表,唯一的循环是有多少“y”值共享相同的“x”值

edit:

编辑:

actually if you wanna take it a step further with reorganizing the data, you could do something like this:

实际上,如果您想更进一步地重新组织数据,您可以执行以下操作:

<script type='text/javascript'>
var list = {
  1 : {
    1 : 'foobar 1,1',
    2 : 'foobar 1,2'
  },
  2 : {
    1 : 'foobar 2,1',
    2 : 'foobar 2,2'
  },
};

</script>

which will allow you to do for instance this

例如,这将允许您执行此操作

var x = 1;
var y = 2;
alert(list[x][y]);