I am trying to add up the ID property from an array of objects. The result displayed is 01395 - instead, it should be 27 (as that's what 13 + 9 + 5 equals).
I believe the code is concatenating the ID properties rather the desired outcome of adding them.
var collection = [{
"Name": "Charlie",
"ID": "13"
}, {
"Name": "Emma",
"ID": "9"
}, {
"Name": "Bob",
"ID": "5"
}];
total = 0, //set a variable that holds our total
id = collection, //reference the element in the "JSON" aka object literal we want
i = 0;
for (i = 0; i < id.length; i++) { //loop through the array
total += id[i].ID; //Do the math!
}
console.log(total); //display the result
JsFiddle: https://jsfiddle.net/3r8vhfb2/