Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with arrays


Our applications will almost always make use of data and in some cases a lot of data. In this section, we will investigate some useful ways to work with collections of data using CoffeeScript and its list comprehension feature and various JavaScript methods made available on the array object.

Iterating over arrays

CoffeeScript provides convenient operators to iterate through collections of data by using loops and comprehensions.

Getting ready

For our example, we will be working with the following array of employee objects:

employees = [
  { id: 10, firstName: 'Tracy', lastName: 'Ouellette', salesYtd: 22246 }
  { id: 2, firstName: 'Chris', lastName: 'Daniel', salesYtd: 3876 }
  { id: 3, firstName: 'Jason', lastName: 'Alexander', salesYtd: 4095 }
  { id: 4, firstName: 'Jennifer', lastName: 'Hannah', salesYtd: 8070 }
  { id: 5, firstName: 'Maxx', lastName: 'Slayde', salesYtd: 2032 }
]

How to do it…

We can iterate through an array using the for item in array format as follows:

#...