Giter Site home page Giter Site logo

hash-iteration-collect-001-prework-web's Introduction

Hash Iteration with #collect

Overview

We'll use #collect to iterate over a hash.

Objectives

  1. Identify when #collect is most often used when iterating over hashes.
  2. Give examples of using #collect to iterate over a hash.

Using #collect

We use #collect to iterate over a collection of data, such as an array or a hash, and return a collection of the data therein.

We have seen it used with arrays to iterate over an array, operate on the data it contains, and return a collection with this altered data.

When working with hashes, we'll most often see #collect used to collect the values of the hash's keys and/or collect data that we've operated on over the course of an iteration.

Let's take a look at an example.

#collecting Hash Values

Let's use #collect to return all of the values of the keys in a given hash.

In this example, we are the managers at Chucky Cheese. Chucky Cheese is a great place to have a birthday party, and there are several birthdays going on here today.

We will be operating on the following hash that tracks birthday kids and their associated ages:

birthday_kids = {
	"Timmy" => 9, 
	"Sarah" => 6, 
	"Amanda" => 27
}

Our managers have asked us to give them the list of ages of the birthday kids so they know how many candles to buy for the birthday cakes. Let's iterate over the birthday_kids hash and collect the ages.

birthday_kids = {
	"Timmy" => 9, 
	"Sarah" => 6, 
	"Amanda" => 27
}

birthday_kids.collect do |kids_name, age|
	age
end

# => [9, 6, 27]

Note that the return value is an array of the values we collected.

Advanced Example

You might have noticed that our previous example didn't operate on the data we were collecting. Let's step it up a level. In this example, we'll iterate over the birthday_kids hash using #collect and return the age of each child, in dog years:

birthday_kids.collect do |name, age|
	age * 7
end

In this case, we are multiplying the value of each age by 7 and collecting the return values of that multiplication into a new array. The above method call would return:

[63, 42, 189]

hash-iteration-collect-001-prework-web's People

Watchers

 avatar  avatar

Forkers

christinechoi

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.