1

I'm trying to populate a multidimensional array (array with array elements) via Javascript or jQuery... push() is probably the wrong method to use, but I've tried a couple of others and I can't seem to get beyond single dimensional arrays. Here's the latest try so far - any advice or snippets on how I might be able to populate arrays per element of an existing global array appreciated!


<script>
var fred=[];
for(i=0;i<3;i++){
    fred.push(['a',i]);
    $("#disp").html(i);
}
alert(fred.toSource());
$("#disp").html(fred.toSource());​
</script>

<div id="disp">abc</div>​


http://jsfiddle.net/eJbm7/

2
  • Can you explain a bit more what you're after? It's very abstract and hard to understand what the goal is at the moment...if you gave an example of what you expect the result to be it could be very helpful. Commented Aug 12, 2010 at 23:13
  • i'd like to be able to assign multiple elements to each index of a main array - similar to what i'd do in php, but in javascript... this way, i can later loop through each "row" to display each array's "columns" Commented Aug 12, 2010 at 23:53

1 Answer 1

0

Array.push should work fine. After all, a two-dimensional array is just an array of arrays.

The link you provided does pop up a message with a two-dimensional array. What's the problem?

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

2 Comments

fred is an array of arrays: [['a', 0], ['a', 1], ['a', 2]]. Maybe you're trying to use the number as a position? Then construct a separate "row" array, fill it up using row.push('abc') and then fred.push(row) for each row.
ahh! sorry my original error had to do with jQuery.inArray not being able to process multidimensional arrays... i had thought it was because my array was not populating.

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.