Coder Social home page Coder Social logo

phase-1-array-find-method-lab's Introduction

Using the Array Find Method

Learning Goals

  • Practice using find() to isolate a specific result

Introduction

We have learned that the indexOf() and find() methods can be used to locate an element in an array, and that they return the first element for which the condition is met. (If you instead want a list of all elements that meet a condition, you would use Array.prototype.filter() instead. We'll learn about filter() in the next lesson.)

We've also learned that the find() method takes a callback function as its argument. This is a common pattern for the Array methods we'll be learning about in this section โ€” in fact, indexOf() is the only one that doesn't take a callback function as an argument. find() is one of two Array methods that expects the callback function to return either true or false (filter() is the other one).

In this lab, we'll practice using the find() method.

Practice Using find() to Isolate a Specific Result

Let's create a function that uses the method find() to single out a specific result in game record data for the Denver Broncos football team. We want to find out if, at any point in the team's existence, they've had a win in the Superbowl. Our data looks like this:

const record = [
  { year: "2015", result: "W"},
  { year: "2014", result: "N/A"},
  { year: "2013", result: "L"},
  //...
]

Write a function called superbowlWin() in index.js:

  • The function should receive 1 argument, an Array of JavaScript Objects
  • Each object has two properties: year and result
  • It should use find() to test each Object to see if the result is "W" โ€” a win!
  • It should return the year when the win occurred (if it occurred at all!)
  • If no win is found, it should return, sadly, undefined

Conclusion

Array.prototype.find() is a built-in function in JavaScript which is used to get the value of the first element in the array that satisfies the provided condition. With this, you can quickly check all the elements of the array and return the first match.

Resources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.