Not too long ago, crazy multi dimensional arrays were all the rage. I mean it’s pretty clear people[0][3] gives me the first name for a person in my array right?? Yeah, um no. Something like people[0].firstName is quite a bit clearer – give me the first name of the first person in my array. This is just a reminder than in you can use keyed objects (or arrays) instead of relying on numeric indexes for everything. Its surprising, but I still see cryptic indexed arrays used even today in coding.
Here’s the cryptic way:
var people = [ [100, 'pgriffin@spoonerst.com', 'Griffin', 'Peter'], [101, 'wretchedwomb@spoonerst.com', 'Griffin', 'Stewie'] ]; p1 = people[0][3]; //first name for Peter p2 = people[1][1]; //email for Stewie
Notice when trying to retrieve the data, the numeric indexes don’t tell us anything. We would have to refer to the original array to try and make sense of the data. And imagine if you have a multidimensional array and have a line of code like people[0][3][12][0][1]. I’ve seen something like that before!
Here’s the easier understand way:
var people = [
{id: 100,
email: 'pgriffin@spoonerst.com',
lastName: 'Griffin',
firstName: 'Peter'},
{id: 101,
email: 'wretchedwomb@spoonerst.com',
lastName: 'Griffin',
firstName: 'Stewie'}
];
p1 = people[0].firstName;
p2 = people[1].email;
The initial object is more code, but accessing the info is way more straightforward and makes the extra overhead worth it.















