2

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.

6
  • 1
    What have you done so far?, we can start from there Commented Jul 2, 2012 at 1:44
  • 1
    Could you have a look at here, stackoverflow.com/questions/11286806/… Commented Jul 2, 2012 at 1:45
  • jQuery has a plugin for that ;) Commented Jul 2, 2012 at 1:47
  • 1
    So this is a duplicate of your own question from an hour ago? Or are you looking for a more general explanation of how JS arrays work? Commented Jul 2, 2012 at 1:54
  • Thank you for reply, just getting confused., cuz I have been told I need to create three arrays first then change the array every single time when you choose different item in the first dropdownlist. Commented Jul 2, 2012 at 1:58

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What I have been told is I need to create three arrays, i am just really confused. Thank you all the same
You don't need 3 arrays. You need one array and store objects created using the syntax above. You could create a multi-dimensional array but it would be a lot more complex than needed.
1

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.

Comments

0

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());

2 Comments

Your string examples are completely mystifying. I think I like them.
@zetlen Thank-you! :) awe*&*someness of hulk :P innit I like your: diplomaticshark.com man nice one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.