How to check if value exist in array using Javascript and Jquery . Javascript and Jquery provides in-built function indexOf() and $.inArray(). In this tutorial, i’ll explain how to use these functions and how we can create our own custom method as well.
Javascript Array and it’s Method.
Difference Between Undefined and Null value in Javascript.
Javascript Custom Method to check Value Exist in Array
To check whether the value exist in array, first method comes in our mind is to use loop and check each value. If we find the match then value exist in array otherwise value does not exist in an array.
In algorithm terms it’s a Linear Search. The time complexity of this method is O(n).
1 2 3 4 5 6 7 8 9 10 11 12 |
/* Declare an array. */ var nameList = new Array('vikash','John','kamal','raj'); /* Traverse each of value of an array using for loop to check whether the value is exist in array*/ for (var i = 0; i < nameList.length; i++) { if (nameList[i] === 'kamal') { alert('Value exist'); } } |
If you don’t know how to create an array in Javascript then check my previous tutorial on Array in Javascript .
How to Reverse String in Javascript.
Let’s create a function which return index of an element if it is found in array otherwise -1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* isExist method two arguements arr - array of values. str - string to be search. */ function isExist(arr,str) { for (var i = 0; i < arr.length; i++) { if (arr[i] === 'kamal') { // Return index return i; } else { /* If value is not found return -1. */ return -1; } } } var nameList=new Array('vikash','John','kamal','raj'); // Call the function var ind = isExist(nameList,'kamal'); console.log (ind); |
Javascript indexOf() method to Check Value Exist in Array
Javascript indexOf() function searches an element in an array. If value exist then it returns the position of an element. Otherwise return -1 if the value is not found.
1 2 3 4 5 |
// Declare an array var nameList = ['jhon','fill','vikash','jammy','tumblr','kamal']; console.log(nameList.indexOf( 'kamal' )); // Print 5 |
1 2 3 4 5 6 7 8 |
if(nameList.indexOf( 'kamal' )>=0){ // If element is found, code to be executed } else { // Code for else case } |
PHP code to check if value exists in Array.
Jquery inArray() method to Check Value Exist in an Array
$.inArray() work similar to indexOf() method. It searches the element in an array, if it’s found then it return it’s index. Return -1 if it’s not found.
1 2 3 4 5 6 7 8 9 10 11 |
/* Let's declare an array. */ var nameList = ['jhon','fill','vikash','jammy','tumblr','kamal']; /* Search vikash in array list. */ console.log($.inArray( 'vikash', nameList )); /* vikash exists in array so it print it's position. */ // Print 2 |
1 2 3 4 5 6 7 8 9 |
/* If condition. */ if($.inArray( 'vikash', nameList )>=0){ // code for true condition }else{ // code for false condition } |
Top websites to Learn Coding Online .
Conclusion
I tried my best to explain the usage of inArray and indexOf method. If you want to add something please let’s us know through your comment so that every reader take the advantage.