jimfog Posted October 27, 2013 Share Posted October 27, 2013 If I wanted to check if a an array contains a specific member....'car' for example. What method should I use to do it? I looked at some methods in w3schools tutorial but I cannot find a method that can perform a specific task. The logic is that if a specific member is found in the array then do ...this. Link to comment Share on other sites More sharing options...
Ingolme Posted October 27, 2013 Share Posted October 27, 2013 Loop through the array until you find the value you're looking for. You can build a function that does it for you function contains(arr, value) { for(var i = 0; i < arr.length; i++) { if(arr[i] == value) { return true; } } return false;}// Use the functionvar N = ["a", "b", "c"];if(contains(N, "c")) { alert("c is in the array");} Link to comment Share on other sites More sharing options...
davej Posted October 27, 2013 Share Posted October 27, 2013 I think most of us programmers would use the obvious approach that foxy mentions above, but I guess you can also use indexOf() since it seems to work on an array as well as on a string... http://www.w3schools.com/jsref/jsref_indexof_array.asp Link to comment Share on other sites More sharing options...
Ingolme Posted October 27, 2013 Share Posted October 27, 2013 I wasn't aware indexOf was also an array method. I can't remember a case in Javascript when I needed to know if an array contained something. I can see why I never learnt it, though: The indexOf() method is not supported in Internet Explorer 8 and earlier. Link to comment Share on other sites More sharing options...
jimfog Posted October 30, 2013 Author Share Posted October 30, 2013 the index of method has a drawback. It returns the index of the member-if that is found of course. So, you have to know from before the possible index of the member you are looking. That index may vary. I would go the function that is depicted above since I only want a true/false and nothing else. It suits my needs better. Link to comment Share on other sites More sharing options...
davej Posted October 30, 2013 Share Posted October 30, 2013 I use indexOf() with strings quite often. If the search string isn't found then the function returns -1, which is easy to test for -- but a loop is a better choice for arrays because of IE8. Link to comment Share on other sites More sharing options...
thescientist Posted October 30, 2013 Share Posted October 30, 2013 (edited) the index of method has a drawback. It returns the index of the member-if that is found of course.So, you have to know from before the possible index of the member you are looking. That index may vary. I would go the function that is depicted above since I only want a true/false and nothing else.It suits my needs better. I'm not sure why you think you need to know the index to use indexOf just like the contains function, since for your requirement you only care if it's in the function or not. From the docs This method returns -1 if the value to search for never occurs.[/size] var N = ["a", "b", "c"];if(N.indexOf("c") >= 0) { alert("c is in the array");} So, really, it just comes down to whether or not you need to support IE8, or you could use a library to normalize browser differences of the implementation http://api.jquery.com/jQuery.inArray/ http://lodash.com/docs#indexOf Edited October 30, 2013 by thescientist Link to comment Share on other sites More sharing options...
jimfog Posted October 30, 2013 Author Share Posted October 30, 2013 I used the in array method after all.Is there any drawback in using it instead of the function above? I am searching for the best alternative here. Link to comment Share on other sites More sharing options...
thescientist Posted October 30, 2013 Share Posted October 30, 2013 not having to maintain it on your own, since you're already using jQuery and they have a much better capacity for regression and unit testing. Link to comment Share on other sites More sharing options...
jimfog Posted October 30, 2013 Author Share Posted October 30, 2013 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now