检查数组的索引并在JQuery中打印变量?

时间:2013-09-28 03:27:16

标签: javascript jquery arrays indexing

我有2个变量数组:

var a = [22, 34, 56,22]
var b = [red, blue, yellow, gray ]

我检查数组a的索引,其中值为22,索引为03,然后我打印var b[0]和{{1}我得到b[3]Red

如何在/ Jquery中执行此操作?

1 个答案:

答案 0 :(得分:0)

你的意思是?

  var a = [22, 34, 56, 22];
  var b = ["red", "blue", "yellow", "gray"];
  var input = 22;

  var matches = $.map(a, function (o, i) { //Using map to get the array of matches, apply map on a
      if (o === input) return b[i]; //check if the value is equal to input, if so return the value from array b for the same index
  })
  console.log(matches); //matches will be array of matches

<强> Fiddle