I have a javascript matrix
array with the following structure (the first index is x
, the second index is y
, so every x
item has 4 y
items):
0
0 { x: 0, y: 0, z: 0}
1 { x: 0, y: 1, z: 5}
2 { x: 0, y: 2, z: 0}
3 { x: 0, y: 3, z: 1}
1
0 { x: 1, y: 0, z: 4}
1 { x: 1, y: 1, z: 5}
2 { x: 1, y: 2, z: 1}
3 { x: 1, y: 3, z: 8}
What I need to do is get an array that stores the z
values per y
value. So for all the values of x
I need the total values of z
arranged by y
. The structure I am looking for (assuming 4 y
values)
0 4
1 10
2 1
3 9
I tried this:
count = [];
$.each(matrix, function(i, j) {
$.each(j, function(k, v) {
count[k] += v["z"];
})
});
But this returns just an array with NaN
values. Does anyone have better suggestions?