0

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?

1
  • What is the error you got?CommentedJan 12, 2013 at 10:06

5 Answers 5

3

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.

2
  • 1
    +1 This is probably what OP needs, even if the lack of described purpose makes it a little unsure.CommentedJan 12, 2013 at 10:12
  • I believe this is what OP meantCommentedJan 12, 2013 at 10:24
2

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"]); 
1
  • @FelixKling You're right, this probably isn't the most useful. I'll upvote your question :)CommentedJan 12, 2013 at 10:11
2

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

    just create the array before adding to it:

    messages['info'] = []; 
      1

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

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.