Linear search in an Array.

Finding that Special Someone

Let's say we're looking for someone in our address book with a specific last name.

We can do this with a technique for searching arrays called "linear search". With it, we use a loop to check through all of the items in the array one-by-one until we see the item that we want.

We can apply linear search to print out all of the people that have a particular last name.

We'll be creating a function that can search for people with a specific last name and print those people out with the printPerson function.

    Create a function called search that takes a parameter called lastName. Leave the list function alone.
    Like with the last exercise, define a variable and store the number of items in the array in it. (Since every function has its own context, or scope, you can call this variable contactsLength, too, if you like!)
    Create a for loop that runs through all of the items in the array. For this step, the code for search is identical to that of list.
    The twist comes here: in the body of the loop, rather than printing out every single item in the array, add an if statement that checks to see if the lastName property of the object is equal to the lastName argument. Have the function run printPerson on the person if and only if the lastName property of the person matches the lastName argument.
    At the bottom of the file, call the search function, passing in "Jones" as the last name to search for.

Source: Codecademy
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
var bob = { firstName: "Bob", lastName: "Jones", phoneNumber: "(650) 777-7777", email: "" }; var mary = { firstName: "Mary", lastName: "Johnson", phoneNumber: "(650) 888-8888", email: "" }; var contacts = [bob, mary]; function printPerson(person) { console.log(person.firstName + " " + person.lastName); } function list() { var contactsLength = contacts.length; for (var i = 0; i < contactsLength; i++) { printPerson(contacts[i]); } } /*Create a search function then call it passing "Jones"*/ function search(lastName) { contactsLength = contacts.length; for (var i = 0; i < contactsLength; i++) { if (lastName === contacts[i]["lastName"]) { printPerson(contacts[i]); } } } search("Jones");
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus