1

So here is my JavaScript array:

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"]
};

Now I get a variable from a PHP form with champion name so e.g. Akali and also with a spell name so e.g. Twin Disciplines now I want to check if that spell and champion exists in my array and if so on which position it is so:

var championName = echo $champion;
var Spell = echo $championSpells[1][$j];
if($.inArray(championName, champions)==-1){
    var existsInArray = false;
} else{
    var existsInArray = true;
}

And at this point im really confused Spell is = 'Twin Disciplnes' championName is = 'Akali' so with those examples I would want to retrive 1 if it was for example 'Mark of the Assassin' I would want to retrieve 2 because it is 2nd in order.

2
  • and what language should be used? Commented Jan 4, 2016 at 16:40
  • @NinaScholz I'm not sure If you can easily use such JS array in PHP so it would be probably easier to do it in JS Commented Jan 4, 2016 at 16:44

1 Answer 1

1

In Javascript you can use Array.prototype.indexOf() for the search of an item in an array. The result is the index starting with 0. If not found, the -1 is returned.

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>');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.