3

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!

1
  • In javascript everything is an object, even functions, arrays and of course objects. There still is a difference, and javascript does'nt have associative arrays, instead it has objects with keys and values, and if that's what you're looking for, an array where you can access values based on keys, you should be using an object. Commented Apr 28, 2013 at 18:59

4 Answers 4

4

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";
Sign up to request clarification or add additional context in comments.

Comments

3

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
});

Comments

2

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.

2 Comments

This is very helpful, thanks. It seems an Array of Objects is the right way to go for me, then. I'm not entirely sure how you would add push the child1 object into a specific index of the parent array, though? Or, does each index then inherit the properties of child1?
@DjangoReinhardt - Each index is going to return the object, array, or whatever is there. So parent[0] === child1. It is basically a pointer to that child1 object. The benefit of using an array is that it is in order and can be accessed by indexes. It must also be what is called "sparse" in that it does not skip indexes. If you wish to use specific indexes or locations, then you probably would want an object as the parent so you could do var parent={};parent["child1"]=child1;parent.child1.name;/*Jim*/. Also available would be the alternate string index - parent["child1"]["name"];//Jim
1

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.

1 Comment

Cool, so an Array of Objects looks like the way to go then. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.