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:
- Loop
- indexOf()
- 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
[pastacode lang=”javascript” manual=”%3Cscript%20type%3D’text%2Fjavascript’%3E%0A%0Avarnames_arr%20%3D%20%5B’sonarika’%2C’yogesh’%2C’sumit’%2C’sonarika’%2C’vijay’%2C’anil’%5D%3B%0A%0AfunctioncheckValue(value%2Carr)%7B%0Avar%20status%20%3D%20’Not%20exist’%3B%0A%0Afor(vari%3D0%3B%20i%3Carr.length%3B%20i%2B%2B)%7B%0Avar%20name%20%3D%20arr%5Bi%5D%3B%0Aif(name%20%3D%3D%20value)%7B%0Astatus%20%3D%20’Exist’%3B%0Abreak%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%0Areturn%20status%3B%0A%7D%0Aconsole.log(‘status%20%3A%20’%20%2B%20checkValue(‘sonarika’%2C%20names_arr)%20)%3B%0Aconsole.log(‘status%20%3A%20’%20%2B%20checkValue(‘mayank’%2C%20names_arr)%20)%3B%0A%3C%2Fscript%3E%0A” message=”” highlight=”” provider=”manual”/]Output:
[pastacode lang=”bash” manual=”status%20%3A%20Exist%0Astatus%20%3A%20Not%20exist%0A” message=”” highlight=”” provider=”manual”/]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 :
[pastacode lang=”javascript” manual=”value-from-which-search.indexOf(%20search-value%20)%3B” message=”” highlight=”” provider=”manual”/]Completed Code:
[pastacode lang=”javascript” manual=”%3Cscript%20type%3D’text%2Fjavascript’%3E%0Avarnames_arr%20%3D%20%5B’sonarika’%2C’yogesh’%2C’sumit’%2C’vijay’%2C’anil’%5D%3B%0Avar%20text%20%3D%20%22SonarikaBhadoria%22%3B%0A%0A%2F%2F%20Search%20in%20Array%0Aconsole.log(%20’Index%20%3A%20’%20%2B%20names_arr.indexOf(‘sumit’)%20)%3B%0Aconsole.log(%20’Index%20%3A%20’%20%2B%20names_arr.indexOf(‘vishal’)%20)%3B%0A%0A%2F%2F%20Search%20in%20String%0Aconsole.log(%20’Index%20%3A%20’%20%2B%20text.indexOf(‘a’)%20)%3B%0A%0A%3C%2Fscript%3E%0A” message=”” highlight=”” provider=”manual”/]Output:
[pastacode lang=”bash” manual=”Index%20%3A%202%0AIndex%20%3A%20-1%0AIndex%20%3A%203%0A” message=”” highlight=”” provider=”manual”/]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:
[pastacode lang=”javascript” manual=”jQuery.inArray(%20search-value%2C%20value-from-which-search)%3B” message=”” highlight=”” provider=”manual”/]Example:
[pastacode lang=”javascript” manual=”%3Cscript%20type%3D’text%2Fjavascript’%3E%0Avarnames_arr%20%3D%20%5B’sonarika’%2C’yogesh’%2C’sumit’%2C’vijay’%2C’anil’%5D%3B%0Avar%20text%20%3D%20%22Yogesh%20singh%22%3B%0A%2F%2F%20Searching%20in%20Array%0Aconsole.log(%20’Index%20%3A%20’%20%2B%20jQuery.inArray(‘sumit’%2C%20names_arr)%20)%3B%0Aconsole.log(%20’Index%20%3A%20’%20%2B%20jQuery.inArray(‘vishal’%2C%20names_arr%20))%3B%0A%0A%2F%2F%20Searching%20in%20String%20variable%0Aconsole.log(%20’Index%20%3A%20’%20%2B%20jQuery.inArray(%20%22e%22%2C%20text%20))%3B%0A%0A%3C%2Fscript%3E%0A” message=”” highlight=”” provider=”manual”/]Output:
[pastacode lang=”bash” manual=”Index%20%3A%202%0AIndex%20%3A%20-1%0AIndex%20%3A%203%0A” message=”” highlight=”” provider=”manual”/]4.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.