Check if values are exists in Array – jQuery and JavaScript

If you are familiar with PHP, where you can use the in_array() function to search value in the Array, and it returns the Boolean value ( TRUE or FALSE ) to Check if values are exists in Array.

There are inbuilt methods in jQuery and JavaScript that return the index position of the value which you can use for the search.

  • how you can check whether an Array already contains a specific value or not.
  • This requires within the program in some cases like-
  • Stop new value from insert if it already exists in an Array.
  • Execute script when the Array contains the particular value.

Methods used to Check if values are exists in Array:

  1. Loop
  2. indexOf()
  3. inArray()

1.Loop

  • First start with a loop.
  • You can easily find the value within an Array by traversing on the Array and check for the value.

Completed Code

<script type='text/javascript'>

varnames_arr = ['sonarika','yogesh','sumit','sonarika','vijay','anil'];

functioncheckValue(value,arr){
var status = 'Not exist';

for(vari=0; i<arr.length; i++){
var name = arr[i];
if(name == value){
status = 'Exist';
break;
    }
  }

return status;
}
console.log('status : ' + checkValue('sonarika', names_arr) );
console.log('status : ' + checkValue('mayank', names_arr) );
</script>

Output:

status : Exist
status : Not exist

For making your searching process simpler you can use jQuery and JavaScript inbuilt function to Check if values are exists in Array

2.Array.indexOf()

  • It is a JavaScript method that returns the index position of the value. If it doesn’t find the value then it returns -1.
  • It works both with string and an Array.

Syntax:

value-from-which-search.indexOf( search-value );

Completed Code:

<script type='text/javascript'>
varnames_arr = ['sonarika','yogesh','sumit','vijay','anil'];
var text = "SonarikaBhadoria";

// Search in Array
console.log( 'Index : ' + names_arr.indexOf('sumit') );
console.log( 'Index : ' + names_arr.indexOf('vishal') );

// Search in String
console.log( 'Index : ' + text.indexOf('a') );

</script>

Output:

Index : 2
Index : -1
Index : 3

3.jQuery.inArray()

  • It is a jQuery function that returns the index position of the value when it finds the value otherwise -1.
  • It also works with string and an Array.

Syntax:

jQuery.inArray( search-value, value-from-which-search);

Example:

<script type='text/javascript'>
varnames_arr = ['sonarika','yogesh','sumit','vijay','anil'];
var text = "Yogesh singh";
// Searching in Array
console.log( 'Index : ' + jQuery.inArray('sumit', names_arr) );
console.log( 'Index : ' + jQuery.inArray('vishal', names_arr ));

// Searching in String variable
console.log( 'Index : ' + jQuery.inArray( "e", text ));

</script>

Output:

Index : 2
Index : -1
Index : 3

Conclusion

  • Some of the methods which you can use to search your value in the existing Array. Using this you can remove or replace the duplicate values.
  • If you are using jQuery within your project then you can simply go with inArray() method otherwise you can indexOf() method.

Chockalingam