简体   繁体   中英

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?

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 .

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"]);

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"]; 

只需在添加数组之前创建数组:

messages['info'] = [];

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM