+ 4

Checking if something is in an array.

How do I do that?

10th Jan 2019, 4:51 PM
Ric2908
Ric2908 - avatar
5 Respostas
+ 11
using indexOf() method It returns the index of the element found. Since arrays starts at 0, if the element is not found it returns -1 function exists(element) { return array.indexOf(element) > -1; } Or just for fun function exists(element) { for(let a = 0; a < array.length; a++) { if(array[a] == element) return a; } return -1; }
10th Jan 2019, 5:12 PM
Toni Isotalo
Toni Isotalo - avatar
+ 6
//It checks if the given particular value is in array or not and bring result to user var users =[ "a", "b", "c", ] if(users. includes("a")) { alert(" found") } else{ alert("not found") }
10th Jan 2019, 6:42 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 3
In additional to Toni Isotalo's answer. Have a look at this snippet👍 https://code.sololearn.com/Wbwu93AL9xpY/?ref=app
10th Jan 2019, 5:50 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar
+ 3
Thank you
12th Jan 2019, 4:40 PM
Mohammad Elahi
Mohammad Elahi - avatar
+ 1
or just for fun
11th Jan 2019, 9:37 PM
Clara Ines Forero Cano
Clara Ines Forero Cano - avatar