Chuck Le Butt
Chuck Le Butt

Reputation: 48798

Javascript multidimensional arrays with alphanumeric keys

This seems to be a common source of confusion from what I've seen, and apparently I'm no exception. I've read a few tutorials on this, and I still can't quite get my head around it. From what I can gather, Arrays are objects in Javascript, just like Strings and other variable types. But I still don't get how that helps me declare a multidimensional array with alphanumeric keys.

In PHP I can simply write:

$calendar = array();

foreach ($schedule->currentExhibitions as $key) {
    $calendar[$key["ExhibitionID"]]["startDate"] = date("Y,n,j", strtotime($exhibition["StartDate"]));
    $calendar[$key["ExhibitionID"]]["endDate"] = date("Y,n,j", strtotime($exhibition["StartDate"]));
} 

But in Javascript trying something similar will create errors. Should I create an Array and fill it will Objects? If so, how would I go about doing so? Or should I just use an Object entirely and skip having any sort of Array? (If so, how do I create a multidimensional Object?)

Sorry for the newbish quesion!

Upvotes: 3

Views: 13451

Answers (4)

Alan
Alan

Reputation: 46853

In Javascript, an array is an object, who's keys are numerical, sequential, indexes.

As soon as you want to use alpha-numerica (aka strings) keys, you use a regular object.

In JS to do what you want, you'd do the following (using more or less your php code).

var calendar = {};

Object.keys(schedule.currentExhibitions).forEach(function(key) {
  var ex = schedule.currentExhibitions[key];

  calendar[ex.exhibitionId] = calendar[ex.exhibitionId] || {}; //if the key doesn't exist, create it.
  calendar[ex.exhibitionId].startDate = date(); //some js date function here
  calendar[ex.exhibitionId].endDate = date(); //your js date function here
});

Upvotes: 3

Travis J
Travis J

Reputation: 82297

I look at Multidimension as nesting, and at multiple levels of nestings as complex objects. For example:

var parent = [];//top holder
var child1 = {};
child1.name = "Jim";
parent.push(child1);

In this simple example, you can access child1 like this:

parent[0]["name"] //Jim

So that is, in a way, multidemensional. Instead of using ["name"] as an indexer, or child1 as an object it could also be an array, like this:

var parent = [];//top holder
var child1 = [];
child1.push("Jim");
parent.push(child1);

In this example, you could get Jim with:

parent[0][0];//Jim

So for complex examples you may have multiple levels of these nestings (or dimensions).

parent[0]["Child"].grandChild[5]["cousin"].name //etc

Where that would just be a continuation of the previous examples down the line.

Upvotes: 2

Frances McMullin
Frances McMullin

Reputation: 5706

If your keys are strictly numerical and ordered starting at zero, then an array makes sense and you can use square bracket notation just like you would in php, although you will need to initialize sub-arrays if you want to have multiple dimensions :

var myArray = [];
myArray[0] = [];
myArray[0][0] = "something interesting";

If your keys are not numerical, ordered and starting at zero, then you should use an object (all keys are strings), which still allows the square bracket notation :

var myObject = {};
myObject["1A"] = {};
myObject["1A"]["3B"] = "something interesting";

Upvotes: 4

jfriend00
jfriend00

Reputation: 707696

If you want to preserve order or you want to access by numeric index, use an array. The value of the array can be a single value or an object or array itself (so each value in the array can contain more than a simple value).

If you want to access by a unique alphanumeric key, then use an object and assign properties to it.

Arrays have numeric indexes. They do not have alphanumeric indexes.

Objects have string keys.

Because an array is also an object, it can have both types of keys, but using a string key is not an array access, it's accessing a property of the object.

When you ask for the .length of an array, you only get the length of the numeric indexes. It does not include other properties of the object.

An array of objects is a very practical data structure in javascript and is used quite often when either order or index by numeric index is important.

If order is not important or you don't need to access by numeric index and just want to access by an alpha numeric string, then you should just use an object and set a properties on it with keys that are your alphanumeric string.

Upvotes: 1

Related Questions