我知道這個問題有點怪,但這裏的例子:使用Javascript/jQuery的從元素位/索引獲取的數組元素索引
這是數組:
mesreponses = ["10700#NB - Purple#1#0", "10699#NB - Nude#1#0", "10698#NB - Navy#1#0", "10697#NB - Marple#1#0", "10696#NB - Grey-Melange#1#0", "10695#NB - Brown-Melange#1#1", "10701#3-6M - Brown-Melange#1#0", "10706#3-6M - Purple#1#0", "10705#3-6M - Nude#1#0", "10704#3-6M - Navy#1#0", "10703#3-6M - Marple#1#0", "10702#3-6M - Grey-Melange#1#0", "10707#9-12M - Brown-Melange#1#0", "10712#9-12M - Purple#1#0", "10711#9-12M - Nude#1#0", "10710#9-12M - Navy#1#0", "10709#9-12M - Marple#1#0", "10708#9-12M - Grey-Melange#1#0", "10713#18-24M - Brown-Melange#1#0", "10718#18-24M - Purple#1#0", "10717#18-24M - Nude#1#0", "10716#18-24M - Navy#1#0", "10715#18-24M - Marple#1#0", "10714#18-24M - Grey-Melange#1#0"]
這裏就是我要做:獲取任何想要的元素的數組位置,例如s_attrs =「NB - Purple」 - 它應該返回0,當搜索「NB-Gray-Melange」時它應該返回1,依此類推。
我設法到目前爲止做:讓元素的索引數組中,即
console.log(String(mesreponses).indexOf(s_attrs));
所以我剛剛得到的索引(即第一個是6,第二28等) - 我可以使用索引來獲取Array的ELEMENT索引嗎?即mesreponses [0],mesreponses [1],mesreponses [2]等
或者我只是想做類似於jQuery.inArray(s_attrs,mareponse)但這不起作用,因爲它只搜索精確火柴。
請問我能做些什麼嗎?
謝謝!