+ 4
Checking if something is in an array.
How do I do that?
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;
}
+ 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")
}
+ 3
In additional to Toni Isotalo's answer.
Have a look at this snippet👍
https://code.sololearn.com/Wbwu93AL9xpY/?ref=app
+ 3
Thank you
+ 1
or just for fun