Need help with javascript .

I have three lists and parameters included in it.

Weapons:

  1. Pistol: usp, glock, p250

  2. Automat: ak47, m4a1, famas

  3. Snipe: awp, scout

Suppose I come from the usp server, I need to bring Pistol , comes ak47 means Automat .

How to solve?

    2 answers 2

     function in_array(item, array) { for(var i = 0; i < array.length; i++) { if(array[i] == item) { return true; } } return false; } var parents = { Pistol: [1, 2, 3], Automat: [4, 5, 6], Snipe: [7, 8, 9] }; var what_do_you_need = 5; // need to return Automat for(key in parents) { if(in_array(what_do_you_need, parents[key])) console.log(key); } 

      Good morning, try to use the Map comparison.

       var guns = [ {firstname : "Automat", lastname: "ak47"}, {firstname : "Pistol", lastname: "ups"} ]; function getFullName(item,index) { var fullname = [item.lastname].join(" "); return fullname; } function myFunction() { document.getElementById("demo").innerHTML = guns.map(getFullName); } 
      • Pavel, if I knew exactly how, I know about the method, but I don’t know how to apply it correctly in this case (an example is needed ... - Members
      • @Members, this should lead you to the right thought - Paulo Berezini
      • Something that is not exactly what is needed, I repeat once again, - Members
      • I’m flying with a match [1] m4a1, I have a list in an array, Automat, it includes the parameters m4a1, ak47 .. the second Pistol, usp, glock in it, so the incoming match with match [1] m4a1 script looks to me Parameters, if this is m4a1, then we output Automat, if glock, then we output Pistol - Members
      • one
        var weapon = 'ak47'; var guns = ["ak47", 'm4a1']; var guns2 = ["usp", 'cl']; var hs = guns.indexOf (weapon)! == -1? true: false; if (hs == true) {console.log ('automat'); } else if (guns2.indexOf (weapon)! == -1? true: false) {console.log ('pistol'); } else {console.log ('awp'); } - Members