walid mahmoud
13 Feb 2022
Javascript
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
13 Feb 2022
Best Answer
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
13 Feb 2022
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
© 2024 Copyrights reserved for web-brackets.com