Reputation: 91
Is it possible to use javascript to populate multidimensional arrays. I want to create three dropdownlists to display "name", "nationality" and "occasion". However, I only can complete a two dimensional arrays.
Any good idea? Thanks a lot.
Upvotes: 2
Views: 5911
Reputation: 2258
There are three different syntax I know for doing this in JavaScript. These Examples assume a 2 by n size array.
1)
var categories = [[]];
var i = 0;
$.each(data, function(key, value) {
categories[i] = new Array(value.cat_id, value.category_label);
i++;
});
2)
var categories = [[]];
var i = 0;
$.each(data, function(key, value) {
categories[i] = [
[value.cat_id],
[value.category_label]
];
i++;
});
3)
var categories = [[]];
$.each(data, function(key, value) {
categories.push([[value.cat_id],[value.category_label]]);
});
Hope this helps.
Upvotes: 1
Reputation: 6516
Literal objects:
var obj = {name : "Bob", nationality : "american", occasion : "often"};
You can access an update JS objects using dot
notation or array notation:
obj["name"] = "Your Name Here";
obj.name = "Your Name Here";
You can push objects into and array to create a collection. Multi-dimensional arrays are not necessary for the task you are describing.
Upvotes: 1
Reputation: 34107
please see this Demo http://jsfiddle.net/LsmYt/ or http://jsfiddle.net/LsmYt/1/
.push
= http://www.w3schools.com/jsref/jsref_push.asp
Hope this helps, please lemme knw if I missed anything!
code
var rambo=[];
for(i=0;i<3;i++){
rambo.push(['hulk ironman',i]);
rambo.push(['Human nationality', i]);
rambo.push(['Yay occassion', i]);
$("#hulk").html(i);
}
alert(rambo.toSource());
$("#hulk").html(rambo.toSource());
Upvotes: 0