2013-09-28 106 views
0

我有2個變量數組:在JQuery中檢查數組和打印變量的索引?

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

我檢查數組a的索引,其中具有值22,索引是03然後我打印var b[0]b[3]和我得到RedGray

我該如何用/在JQuery中做到這一點?

+0

很抱歉,但你想要什麼?什麼是「如何使用JQUERY Script」? – Blaise

+0

** jQuery **,而不是JQUERY。這不是一個縮寫。 – meagar

+0

對不起,我的英語:(我想在程序化jQuery中,你能幫我嗎? – bukanamay

回答

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

+0

非常感謝,其工作:) – bukanamay

+0

@bukanamay歡迎您。 – PSL