walid mahmoud
Published in : 2022-02-13
This is the only way I know to do it:
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
Is there a better and more concise way to accomplish this?
Yasen Sayed Date : 2022-02-13
Best answers
8
Best answers
8
You can use this in the modern browsers, code to check the array
console.log(['joe', 'jane', 'mary'].includes('jane')); //true
Also, you can the indexOf method like this:
console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); //true
Joseph Morgan Date : 2022-02-13
Best answers
11
Best answers
11
let arr = ['orange', 'blue', 'red'];
let index = arr.indexOf('blue');
if(index > -1){
//Exists
console.log(arr[index]);
}
also, you can use this way using indexOf
Join our community and get the chance to solve your code issues & share your opinion with us
Sign up Now