Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

    Array Comprehensions - NON-STANDARD

    John LindquistJohn Lindquist

    Array Comprehensions didn't make the ES6 cut. You probably shouldn't use them ;)

    See comments.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    Narrator: 00:00 If I have an array of people and I want to get all of their email addresses into a separate array, I can use array comprehension. For and then deconstruction, email of people. Give me all of the emails. Then from here I can just log them out, run this, and you can see I get each of their emails.

    00:22 Now if I want to only get the email if the person's name is Melinda, I can get the email and the first name. Then I can say if first name is Melinda, then it would return the email, and I'll log it out. You can see I get the two email addresses of this Melinda and of this Melinda.

    00:52 To do something a little simpler, let's get all of the numbers of above five, so we'll say four and then num of nums. Now here I'm not using the deconstruction this time. I'm just getting the actual item that comes back.

    01:08 I'll say if num is greater than five, then give me the number. I only want the numbers in this that are greater than five, I'll log it out, and I'll get 6 through 10. Obviously if instead I said less than five, then I ran it, you could see I'd get one through four.

    01:28 If you've worked with arrays before in this fashion, whether using a polyfill, your own thing, underscore or whatnot, this probably looks familiar from mapping and filtering and all the functions you've probably used in the past. It's just that now ES6 has a built-in syntax to handle these scenarios.

    01:44 As our final example, let's take the numbers and the letters, so we'll say four num of nums, and for letter of letters. Then we'll return the number plus the letter and you can see now we'll get a one-dimensional array from 1A going all the way down to 5E. Or if I wanted to, I could surround this with the array brackets.

    02:16 Once I run this again, I'll get a grid this time of a two-dimensional array with arrays inside of it for each row to represent the rows and the columns.