Finding an Element in a List

You will often need to search through the contents of arrays to find where specific elements occur. Take this grocery list as an example:

var list = ["bread", "eggs", "milk", "cookies", "bananas", 
    "tuna", "lettuce", "yogurt", "cheese", "chicken", "cucumbers", 
    "orange juice", "salt", "doritos", "lemonade", "apples", 
    "paper towels", "red onion", "minced garlic", "tumeric", 
    "donuts", "bagels", "crackers", "red pepper", "green pepper",
    "spinach", "canola oil", "vanilla", "flour", "brown sugar", 
    "powdered sugar", "lime"];

Suppose you want to find the location of where "bread" occurs in this array. Or suppose you want to find the position of where "paper towels" occurs in the array. How can you do this without having to manually count through each position of the array yourself?

You will recall that each individual location or position in an array is called an index. The starting index in an array is always 0. Thus, "bread" occurs in this array at index 0. The remaining indices are numbered consecutively up until the last element in the array. For example, "lettuce" occurs at index 6.

You can find the index of any element in an array by writing your own indexOf() function.

Writing an Array indexOf Function

Let's say we are given this as a start() function to work with:

function start(){
    var list = ["bread", "eggs", "milk", "cookies", "bananas", 
    "tuna", "lettuce", "yogurt", "cheese", "chicken", "cucumbers", 
    "orange juice", "salt", "doritos", "lemonade", "apples", 
    "paper towels", "red onion", "minced garlic", "tumeric", 
    "donuts", "bagels", "crackers", "red pepper", "green pepper",
    "spinach", "canola oil", "vanilla", "flour", "brown sugar", 
    "powdered sugar", "lime"];

    var idx = indexOf(list, "bread");
    println("The index of bread is: " + idx);
}

This code will not run because we first need to define our own indexOf() function:

The indexOf() function takes in an array, arr, and a string to search for within the array, str. We begin by looping through the contents of the array using for loop iteration. On each iteration, we get the current element using arr[i] and store it into a variable called cur. We then check to see if the current element, cur, is equal to the string we are searching for, str. If it is, we return the index as kept track by our for loop variable, i.

However, if the element is not found after searching through the entire array, we return -1. Why -1? Because an array's index can't be negative! This tells us that the element is not in the array at all.

Putting It All Together

Now that we have our start() and indexOf() functions combined and thoroughly explained, let's verify that it works. What will get printed to the screen after running this program?

The program prints:

The index of bread is: 0

More Practice with Searches

Using the same grocery list and indexOf() function, let's get a little more practice searching through arrays.

We already know that "bread" occurs at index 0. Here we are also finding the indices for "lettuce", "paper towels", and "apple juice". What will get printed to the screen? What index values will be returned for each of these things?

Answer:

Since apple juice is not in the list at all, it returns -1.

JavaScript has its Own Built-in indexOf Method!

Javascript has an indexOf method of its own:

arrayName.indexOf(str);

We can use this to ensure the results match with the indexOf() function that we built ourselves.

Resulting output:

We have thus verified that the two methods are indeed identical.

Last updated

Was this helpful?