I am having a heck of a time trying to figure out how to create a multidimensional array in jQuery.
I instantiate the array outside of the loop.
<script>
var myArray = [];
</script>
Inside of my loop I want to add the array elements.
i = 0
[loop start]
<script>
myArray[i][$row[sku]] = $row[qty]; // sku might be repeated will this cause an issue? You will see in the error below "295518" is repeated...
<script>
[loop end]
In my source code it looks like this:
<script>
myArray[ 1 ][ 295518 ] = 122;
</script>
Then I run this at the end outside the loop...
<script>
console.log( myArray );
</script>
I get this error in the console:
Uncaught TypeError: Cannot set property '295518' of undefined
Uncaught TypeError: Cannot set property '70252' of undefined
Uncaught TypeError: Cannot set property '295518' of undefined
What I am doing wrong in setting up this array? Thanks!
asked Feb 6, 2014 at 4:56
LargeTuna
2,8448 gold badges50 silver badges97 bronze badges
-
show your code of array...user2727841– user27278412014年02月06日 05:01:28 +00:00Commented Feb 6, 2014 at 5:01
2 Answers 2
you can do so:
var a = [];
a[0] = [1,2,3];
a[1] = [4,5,6];
a[1][1] it is 5
answered Feb 6, 2014 at 5:05
A.T.
26.7k8 gold badges50 silver badges68 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Alan
Excellent, a tiny example like this, of actual code that works, is ideal - this solved what I was looking for where 45mins of Ggl and lots of SE pages didn't -- thank you.
It's a two dimensional array,
You can define it like this
var myArray = ['',''];
Adrian World
3,1262 gold badges19 silver badges25 bronze badges
answered Feb 6, 2014 at 5:13
Dimag Kharab
4,5391 gold badge27 silver badges47 bronze badges
Comments
Explore related questions
See similar questions with these tags.
default