< Return to Video

5.2: Associative Arrays in JavaScript - Programming from A to Z

  • 0:00 - 0:05
    Hello in this video I'm going to talk
    about something called, how to clean
  • 0:05 - 0:09
    your glasses. Theres a horrible smudge on
    my glasses, this is what I look like
  • 0:09 - 0:13
    without glasses by the way. I'm going to
    talk about something called an associative
  • 0:13 - 0:18
    array. Forevermore associative arrays will
    be associated with me cleaning my glasses
  • 0:18 - 0:23
    what are you gonna do? What is an
    associative array. Ok lets talk about
  • 0:23 - 0:27
    first, and why do we need one? First
    of all, the context here is I want to do
  • 0:27 - 0:32
    a word counting application. Meaning,
    I want to have a list of every word thats
  • 0:32 - 0:37
    in a document and a number associated
    with that word, a list of pairs of things.
  • 0:37 - 0:43
    Word, number, word, number, key, value
    key, value.This is sort of where I'm going
  • 0:43 - 0:50
    But first before I even get there, lets
    talk about what is an array. Which if
  • 0:50 - 0:54
    you're watching this video hopefully
    you're familiar with, if not don't worry
  • 0:54 - 1:00
    life will be ok. I might refer you to one
    of my videos about arrays. But an array is
  • 1:00 - 1:04
    ultimately just a list of things, ordered
    list of stuff. Look at this marker its
  • 1:04 - 1:12
    excellent! So maybe I might have some
    numbers like 3, 4, 19, -7, 29. Now this
  • 1:12 - 1:17
    is an ordered list, meaning every single
    number has an index. And when you count
  • 1:17 - 1:21
    with arrays in programming, generally
    speaking you start counting with 0.
  • 1:21 - 1:33
    0,1,2,3,4. So here is my array, 3,4,19,-7
    29, ordered in order 0,1,2,3,4.
  • 1:33 - 1:37
    So this is a regular array, a list
    ordered list. An Associate array also
  • 1:37 - 1:42
    by the way if you're hearing a strange
    buzzing in your ear its not just you its
  • 1:42 - 1:45
    construction thats happening right outside
    the window over there while I'm recording
  • 1:45 - 1:49
    this video. But this is life in new york
    city, people like to complain about
  • 1:49 - 1:52
    construction happening in their backyard
    but if you live here, you signed up for it.
  • 1:52 - 2:07
    Associate Array! Lets say I have these
    same numbers, I'm gonna draw them like
  • 2:07 - 2:12
    this because even though I'm kinda giving
    them spatial orientation it appears they
  • 2:12 - 2:15
    may have some sort of order, this new
    thing I'm going to talk about, Associate
  • 2:15 - 2:21
    array is not really ordered. So the way
    that i access, if this is called Array.
  • 2:21 - 2:26
    If this is a variable storing that, the
    way that I would access the number 19.
  • 2:26 - 2:33
    Would be array index 2. With an
    associative array, this is an associative
  • 2:33 - 2:36
    array, I'm associating numbers with
    numbers, but really what I'm talking
  • 2:36 - 2:41
    about with an Associative array is being
    able to associate a key that is not just
  • 2:41 - 2:46
    an index with a particular number. That
    key in many cases is a string. And all
  • 2:46 - 2:50
    the examples I'm going to show you use a
    string. Of course the key can be something
  • 2:50 - 2:54
    other than a string, a string of text. So
    the idea is here what if instead of
  • 2:54 - 3:00
    referring to each of these by their index
    I could name them. So this one is rainbow
  • 3:00 - 3:11
    And this one is unicorn. This one is
    purple, this one is heart, Oh I ran out
  • 3:11 - 3:19
    of my happy words. This one is happy.
    Happy little associative array. For all
  • 3:19 - 3:23
    you wanting me to make Bob Ross references
    So this is the idea, what if I could
  • 3:23 - 3:28
    instead of having an ordered list with
    indexes, what if I could name all these
  • 3:28 - 3:38
    elements. If this is my associative array,
    assoc, my associative array, I would say
  • 3:38 - 3:50
    Associative index unicorn. I could put
    the name in the square brackets to
  • 3:50 - 3:55
    indicate thats how I want to look up
    this particular value. So why is this
  • 3:55 - 4:01
    useful? I think I need to make a separate
    side video related to this one I'm doing
  • 4:01 - 4:06
    right now, to discuss when would you use
    and ordered list, and when would you use
  • 4:06 - 4:12
    an associative array. In this case, of
    word counting you can clearly see why
  • 4:12 - 4:16
    I might want to use an associative array.
    because i want to keep track of words
  • 4:16 - 4:20
    and the number of times they appear
    in a given document. That noise is really
  • 4:20 - 4:28
    driving me crazy. I have to zen out for a
    second. Lets just say what I wanted to do
  • 4:28 - 4:37
    was pair words with numbers. I could
    say something like unicorn colon 16 comma
  • 4:37 - 4:48
    rainbow colon 2 comma heart colon 10.
    You could see how this like a nice list
  • 4:48 - 4:56
    of key value pairs, this being the key to
    look up a particular value, 16.
  • 4:56 - 5:02
    Lets put some more syntax on this, Oh
    I'll just put a curly bracket here, and
  • 5:02 - 5:09
    I'll put a curly bracket here and I'll say
    var dictionary equals this. If you're
  • 5:09 - 5:16
    looking at this what you'll notice is
    thats the syntax of a Javascript object!
  • 5:16 - 5:20
    So a javascript object is exactly this.
    I think i have a video that talks about
  • 5:20 - 5:24
    what is a javascript literal object.
    I think what i say is its a collection of
  • 5:24 - 5:30
    name value pairs. So in fact we can simply
    use the fact that a javascript object
  • 5:30 - 5:34
    natively is a collection of name value
    pairs to act as a dictionary. Now there
  • 5:34 - 5:40
    are some tricks I'm going to employ to
    make it usable. How do i sort its order,
  • 5:40 - 5:44
    javascript objects don't actually have an
    order to them. How do i iterate over all
  • 5:44 - 5:48
    the possibilities? We're going to see that
    as I build out the coding challenge, doing
  • 5:48 - 5:53
    a word counting application. But lets just
    examine, theres one piece of this which
  • 5:53 - 5:56
    is kind of crucial that I want to examine.
    Let me go back over to the code for a
  • 5:56 - 6:04
    second. I'm going to go over to a browser
    instance, because I'm going to work just
  • 6:04 - 6:09
    in the developer console to show you
    what i mean. Lets say I want to create
  • 6:09 - 6:16
    this object called dictionary. The first
    thing I can do if I want to create an
  • 6:16 - 6:21
    empty object is I can say open curly
    bracket closed curly bracket. Let me
  • 6:21 - 6:26
    make that a little bit bigger for you,
    open curly bracket, closed curly bracket.
  • 6:26 - 6:32
    Now i have an object which is empty. What
    if i want to say the number of times
  • 6:32 - 6:37
    rainbow appeared in that dictionary is 12.
    Well you know I can say dictionary dot
  • 6:37 - 6:43
    rainbow equals 12. Now if i look at the
    dictionary you can see, let me just do
  • 6:43 - 6:48
    this again, you can see look, the object
    now has the word rainbow associated with
  • 6:48 - 6:54
    the number 12. Lets say Im trying to
    track, what i actually want to look for
  • 6:54 - 7:01
    is the number of times a writer uses a
    contraction, like can't or don't versus
  • 7:01 - 7:10
    the longer way of writing that out. Can
    not or do not. Dictionary dot can't equals
  • 7:10 - 7:17
    15 because the Author that I'm analyzing
    used the word can't 15 times.
  • 7:17 - 7:22
    Uncaught syntax error, invalid or
    unexpected token, so this seems to
  • 7:22 - 7:27
    have broken my idea. Because thats
    an invalid variable name. But I have
  • 7:27 - 7:31
    something that might want to keep
    track of its count. Right? I want to know
  • 7:31 - 7:36
    I also might want to know how many times
    did the area code 917 appear in a text
  • 7:36 - 7:41
    Thats not a valid variable name either
    I don't think,Right? So if I come back
  • 7:41 - 7:47
    over here and say dictionary dot 917
    equals it appears 25 times. Thats also
  • 7:47 - 7:51
    invalid. One of the things thats really
    important here in how things work in
  • 7:51 - 8:06
    Javascript, is that you can access
    the properties of an object in two ways.
  • 8:06 - 8:21
    I can say dictionary dot heart equals 15
    or I can say dictionary string heart
  • 8:21 - 8:28
    equals 15. These are equivalent. The
    heart property of dictionary has the value
  • 8:28 - 8:33
    15, or the hart property in quotes in
    square brackets of dictionary has the
  • 8:33 - 8:40
    value 15. the question becomes well
    this maybe looks more syntactically
  • 8:40 - 8:43
    concise, and it might be what you're
    familiar with. I make a particle system
  • 8:43 - 8:48
    particle dot x its 20, particle dot y,
    particle dot speed. But this is actually
  • 8:48 - 8:52
    something, this is the syntax we'll
    want to use for word counting applications
  • 8:52 - 9:00
    Why? because this will work with something
    like can't or 917. We can make any string
  • 9:00 - 9:05
    inside here and set it as a valid value
    of the dictionary. And in fact even more
  • 9:05 - 9:09
    important what you're going to see that I
    need to do in the word counting coding
  • 9:09 - 9:14
    challenge, is I'm gonna need to have a
    variable. I'm reading a text in, so I
  • 9:14 - 9:18
    don't actually have that string in my code
    its in a variable. So if I have a variable
  • 9:18 - 9:26
    name I can't say dictionary dot variable
    name I have to say dictionary bracket
  • 9:26 - 9:29
    the variable name if its a string in that
    variable. Let me make that more clear by
  • 9:29 - 9:35
    showing you this couple steps through
    again here in the console. So first, lets
  • 9:35 - 9:44
    confirm that it works, if I say dictionary
    917, equals 15 that works, and I say
  • 9:44 - 9:51
    dictionary can't, now notice I switched to
    using double quotes, because theres a
  • 9:51 - 9:55
    single quote in can't and so it'll get
    confused it I use single quotes and a
  • 9:55 - 9:58
    single quote, but I can just use double
    quotes and it'll fix that problem.
  • 9:58 - 10:05
    I could do this, and now lets look at the
    dictionary and you can see, look what
  • 10:05 - 10:10
    its got, its got 917 and its got can't
    in it. So you can see here that its
  • 10:10 - 10:14
    able to keep those as names of the
    properties if I pass them in on a
  • 10:14 - 10:20
    string. Now let me talk about what I mean
    by a variable. Lets say var word equals,
  • 10:20 - 10:25
    did i do unicorn yet, I don't think so.
    Var word equals unicorn, well what
  • 10:25 - 10:31
    happens if I say dictionary dot word
    equals 99. Whats gonna be in there?
  • 10:31 - 10:36
    Is it gonna be 99 associated with the
    string unicorn or is it gonna be 99
  • 10:36 - 10:41
    associated with something called word.
    Lets see what happens, ok now if I look
  • 10:41 - 10:47
    at dictionary, unicorns not in there, it
    thinks word is the string. It doesn't know
  • 10:47 - 10:52
    that word is that variable, its like I
    can't even explain it because it doesn't
  • 10:52 - 10:55
    make sense in my head even though
    it seems like maybe that would work.
  • 10:55 - 11:02
    I have to use that other syntax, because
    what I want to do is say var word equals
  • 11:02 - 11:07
    heart then I can say dictionary bracket
    word, now its gonna evaluate the property
  • 11:07 - 11:12
    of that variable as heart and be able to
    set that number. I could say 100 and I
  • 11:12 - 11:18
    could look at my dictionary again and we
    could see there are all the words and
  • 11:18 - 11:22
    their associated counts. Heart is in there
    at 100, thats the thing I'm gonna need
  • 11:22 - 11:26
    to do because I'm reading in a text
    and pairing the values with it.
  • 11:26 - 11:31
    Ok so i think this defines basically the
    idea of what an associative array is,
  • 11:31 - 11:36
    and how you can use one in javascript.
    Now how you iterate over it, how you
  • 11:36 - 11:40
    might sort it, how you kind of mess with
    it, these are things that I will show you
  • 11:40 - 11:44
    in the next video which is gonna be a
    coding challenge of doing a word
  • 11:44 - 11:48
    concordance. Thanks for watching and
    see you soon.
Title:
5.2: Associative Arrays in JavaScript - Programming from A to Z
Description:

more » « less
Video Language:
English
Duration:
11:57

English subtitles

Revisions