-3

Possible Duplicate:
How do I get the index of an item in an array?
search for element within an array

If I had an array like this:

var array = [
    "one",    //element 0
    "two",    //element 1
    "three",  //element 2
    "four",   //element 3
    "five"    //element 4
];

I want to look for a given part, let's say five. So I ask the code to tell if five is inside array.

If it is, then it should tell me that it's position is 4, or array[4].

Is there any way I could achieve a similar effect?

7
  • 2
    What have you tried? Did you make an attempt to find the answer yourself? This is not an unusual situation, and there's a method specifically designed to handle this. Commented Oct 2, 2012 at 14:01
  • 1
    indexOf is what you want Commented Oct 2, 2012 at 14:01
  • @user1689607 there's a method specifically designed for this on newer browsers Commented Oct 2, 2012 at 14:03
  • In a Collaborative Question and Answer nobody asks the code. Please read the FAQ. It is important to try something by yourself and post a specific question related to your code. Commented Oct 2, 2012 at 14:03
  • 1
    How do I get the index of an item in an array? Commented Oct 2, 2012 at 14:04

3 Answers 3

8

array.indexOf()

Use the shim included in that link if you're on an older browser.

Sign up to request clarification or add additional context in comments.

1 Comment

Will be accepted as soon as I can.
3
var array = [
    "one",    //element 0
    "two",    //element 1
    "three",  //element 2
    "four",   //element 3
    "five"    //element 4
];
alert(array.indexOf("five"));

this will alert 4, which is "five"

Comments

0

use compare function of string to look if there is a specific string inside your array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.