admin管理员组文章数量:1316023
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?
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?
Share Improve this question asked Jan 12, 2013 at 10:05 Alan CoromanoAlan Coromano 26k55 gold badges139 silver badges214 bronze badges 1- What is the error you got? – Felix Kling Commented Jan 12, 2013 at 10:06
5 Answers
Reset to default 3You 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"];
just create the array before adding to it:
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 (remended):
var messages = {};
messages['info'] = ["info message1", "info message2", "info message3"];
messages['error'] = ["error message1", "error message2", "error message3"];
本文标签: Create an array of hashes in javascriptStack Overflow
版权声明:本文标题:Create an array of hashes in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741983401a2408533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论