views:

62

answers:

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/

A: 

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?

Gintautas Miliauskas
`$("#disp").html(fred.toSource());​` is not returning an array...
ina
`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.
Gintautas Miliauskas
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.
ina