0

I am trying to create a multidimenssional array like this

var myArray = new Array();
          var test = new Array(1, 100,200,2);
        $.each(test, function(index, val) {

            myArray['value'].push(val);
            myArray['index'].push(index);
        });

but console.log(myArray) shows me no values;

1
  • JavaScript doesn't have multidimensional array. Commented Oct 13, 2012 at 1:59

1 Answer 1

2

Use an object literal instead of an array:

var myHash= {
    value: [],
    index: []
},
test = [1, 100,200,2];

$.each(test, function(index, val) {
    myHash['value'].push(val);
    myHash['index'].push(index);
});

You shouldn't use arrays as associative arrays. Arrays are accessed by 0-based index and you shouldn't assign arbitrary properties to arrays.

Example: http://jsfiddle.net/andrewwhitaker/F7Zx5/

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

5 Comments

this doesn't create a multidimentional array
I'm pretty sure the OP is misusing the term "Multidimensional Array"
can I use object instead of an array for this
and if I can then how? since that article makes sense of not using associative arrays in js
Yes... My answer outlines how to accomplish that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.