Alan Coromano
Alan Coromano

Reputation: 26048

Create an array of hashes in javascript

I want to create an array of hashes in javascript. In other words, I want to do the following thing

 var messages = new Array;
 messages['info'].push(["info message1", "info message2", "info message3"]);  
 messages['error'].push(["error message1", "error message2", "error message3"]); 

and then iterate through each key. But it gives me an error "Cannot call method 'push' of undefined"

How can I do it?

Upvotes: 0

Views: 5564

Answers (5)

Felix Kling
Felix Kling

Reputation: 817030

You are trying to access the property info of messages, which does not exists, hence its value is undefined. You are then trying to treat it as an array by calling .push. That won't work.

I think what you actually want is to assign the arrays to each of those properties:

var messages = {};
messages['info'] = ["info message1", "info message2", "info message3"];  
messages['error'] = ["error message1", "error message2", "error message3"];
// or
// messages.info = ["info message1", "info message2", "info message3"];
// ...

Only use arrays with numeric keys. Use plain objects for string keys.

Now that messages.info is defined and as in array, you can add new messages to it:

messages.info.push('some new message');

Learn more about objects.

Upvotes: 3

jfriend00
jfriend00

Reputation: 708046

You have to create an empty array before you can call .push() on it. In addition, arrays are designed for numeric index access. If you want to access messages by property names like 'info', then you should use an object instead of an array:

 var messages = {};
 messages['info'] = [];
 messages['info'].push(["info message1", "info message2", "info message3"]);  
 messages['error'] = [];
 messages['error'].push(["error message1", "error message2", "error message3"]); 

or a little more concise:

 var messages = {};
 messages['info'] = ["info message1", "info message2", "info message3"];
 messages['error'] = ["error message1", "error message2", "error message3"]; 

Upvotes: 2

Denys Séguret
Denys Séguret

Reputation: 382454

You also have to create the arrays in the main array/object :

var messages = []; // you probably shoudln't have an arrray but {}
messages['info'] = [];
messages['info'].push(["info message1", "info message2", "info message3"]);

Upvotes: 2

Mattias Buelens
Mattias Buelens

Reputation: 20179

You didn't define messages['info'] or messages['error'] before using it. Initialize it first. Also, arrays should not be used to store key/value mappings, use a plain object for that.

var messages = new Object;
messages['info'] = new Array;
messages['info'].push("info message1", "info message2", "info message3");
messages['error'] = new Array;  
messages['error'].push("error message1", "error message2", "error message3");

Note that you had another error in your original code, namely you were passing an array to .push(), which would result in an array of arrays of arrays.

Or using object and array literals (recommended):

var messages = {};
messages['info'] = ["info message1", "info message2", "info message3"];
messages['error'] = ["error message1", "error message2", "error message3"];

Upvotes: 1

keune
keune

Reputation: 5795

just create the array before adding to it:

messages['info'] = [];

Upvotes: 1

Related Questions