我有2个变量数组:
var a = [22, 34, 56,22]
var b = [red, blue, yellow, gray ]
我检查数组a
的索引,其中值为22
,索引为0
和3
,然后我打印var b[0]
和{{1}我得到b[3]
和Red
。
如何在/ Jquery中执行此操作?
答案 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 强>