From arrays to objects
As you already know from Chapter 2, Primitive Data Types, Arrays, Loops, and Conditions, an array is just a list of values. Each value has an index (a numeric key) that starts from zero and increments by one for each value. Consider the following example:
> var myarr = ['red', 'blue', 'yellow', 'purple']; > myarr; ["red", "blue", "yellow", "purple"]. > myarr[0]; "red" > myarr[3]; "purple"
If you put the indexes in one column and the values in another, you'll end up with a table of key/value pairs shown as follows:
Key |
Value |
0 |
red |
1 |
blue |
2 |
yellow |
3 |
purple |
An object is similar to an array, but the difference is that you define the keys yourself. You're not limited to using only numeric indexes, and you can use friendlier keys such as first_name
, age
, and so on.
Let's take a look at a simple object...