所以这是我的JavaScript数组:
var champions = {
"Aatrox":["Blood Well","Dark Flight", "No Q2", "Blood Thirst", "Blood Price", "Blades of Torment", "No E2", "Massacre"],
"Ahri":["Essence Theft","Orb of Deception", "Fox-Fire", "Charm", "Spirit Rush"],
"Akali":["Twin Disciplines", "Mark of the Assassin", "Twilight Shroud", "Crescent Slash", "Shadow Dance"],
"Alistar":["Trample","Pulverize","Headbutt","Triumphant Roar","Unbreakable Will"],
"Amumu":["Cursed Touch","Bandage Toss","Despair","Tantrum","Curse of the Sad Mummy"]
};
现在我从一个带有冠名的PHP表单中获取一个变量,例如Akali
以及拼写名称,例如Twin Disciplines
现在我想检查我的数组中是否存在该咒语和冠军,如果是,那么它是在哪个位置:
var championName = echo $champion;
var Spell = echo $championSpells[1][$j];
if($.inArray(championName, champions)==-1){
var existsInArray = false;
} else{
var existsInArray = true;
}
此时我真的很困惑Spell is ='Twin Disciplnes'goldName = ='Akali'所以我想要回顾1,如果它是'刺客的标记'我想要检索2因为它排在第二位。
答案 0 :(得分:1)
在Javascript中,您可以使用Array.prototype.indexOf()
搜索数组中的项目。结果是索引以0
开头。如果未找到,则返回-1
。
var champions = {
"Aatrox": ["Blood Well", "Dark Flight", "No Q2", "Blood Thirst", "Blood Price", "Blades of Torment", "No E2", "Massacre"],
"Ahri": ["Essence Theft", "Orb of Deception", "Fox-Fire", "Charm", "Spirit Rush"],
"Akali": ["Twin Disciplines", "Mark of the Assassin", "Twilight Shroud", "Crescent Slash", "Shadow Dance"],
"Alistar": ["Trample", "Pulverize", "Headbutt", "Triumphant Roar", "Unbreakable Will"],
"Amumu": ["Cursed Touch", "Bandage Toss", "Despair", "Tantrum", "Curse of the Sad Mummy"]
};
function getPos(o,n, s) {
return n in o && o[n].indexOf(s);
}
document.write(getPos(champions, 'Akali', 'Twin Disciplines') + '<br>');
document.write(getPos(champions, 'Akali', 'Mark of the Assassin') + '<br>');
&#13;