Reputation: 49
I have this express route that returns me the number of carnapping crimes per month. In the variable records, to explain it further, {"1":[{"1":2}]
, where the first 1 is query#1 and the next 1 indicates as January as the 1st month of the year. and lastly, 2 which indicates the number of crimes in the month of January.
app.get('/carnapping-route', function(req, res){
var records1 = [{"1":[{"1":2}],"2":[{"2":1}],"3":[{"3":2}],"4":[{"4":1}],"5":[{"5":1}],"6":[{"6":1}],"7":[{"7":1}],"8":[{"8":1}],"9":[{"9":1}],"10":[{"10":1}],"11":[{"11":1}],"12":[{"12":1}]}];
var counts = [ ];
for (var i = 1; i <= 12; i++) {
var key = i + "";
counts.push(records[0][key][key]);
}
res.send(counts);
});
Instead of getting [2,1,2,1,1,1,1,1,1,1,1,1]
, I get [null,null,null,null,null,null,null,null,null,null,null,null]
What did I missed?
Upvotes: 0
Views: 56
Reputation: 1333
In your data structure you have an array in records[0][key]
variable. So you need to use records[0][key][0][key]
in push
method
var records1 = [{"1":[{"1":2}],"2":[{"2":1}],"3":[{"3":2}],"4":[{"4":1}],"5":[{"5":1}],"6":[{"6":1}],"7":[{"7":1}],"8":[{"8":1}],"9":[{"9":1}],"10":[{"10":1}],"11":[{"11":1}],"12":[{"12":1}]}];
var counts = [ ];
for (var i = 1; i <= 12; i++) {
var key = i + "";
counts.push(records1[0][key][0][key]);
}
//res.send(counts);
document.getElementById("result").innerHTML=counts;
<span id="result"></span>
Upvotes: 1