I get a JSON object from PHP that holds multiple rows like this:
id | type | amount| age
1 | abc | 1 | 1
2 | def | 2 | 1
3 | def | 4 | 2
4 | def | 13 | 3
I run it through JavaScript:
$.ajax({
type: 'POST',
url: "test.php",
dataType: 'json',
success: function (data) {
var arr = [];
for (var i = 0; i < data.length; i++) {
arr[data[i].type][data[i].age] = data[i].ammount;
}
}
});
The idea is to get array that looks like this:
arr[def][1] = 2
arr[def][2] = 4
arr[def][3] = 13
arr[abc][1] = 1
arr[abc][2] = 0 // note that this one is just empty because it was not declared from json data.
Unfortunately I get an error:
Uncaught TypeError: Cannot set property '1' of undefined
1 is [data[i].age]
and if I change its value error appears with other number.