Jump to content

searching for a specific array member


jimfog

Recommended Posts

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

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

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

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

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

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 by thescientist
Link to comment
Share on other sites

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

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

:Pleased:

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...