Reputation: 123
I return the following JSON to populate a highchart http://www.highcharts.com/
[["Graz", 5.0],["Rio de Janeiro", 3.0],["Bräcke", 2.0],["Campinas", 2.0],["Colchester", 2.0],["Cunewalde", 2.0],["Lille", 2.0],["London", 2.0],["Charleroi", 1.0],["Caracas", 1.0],["Butte", 1.0],["Buenos Aires", 1.0]]
rather than requery the database again. How would I get the total of the values returned? ie in this case 24. I am still getting used to JSON, so would appreciate any help anyone can give...thanks
Upvotes: 0
Views: 2805
Reputation: 53888
since the question has got a jq
tag, here it goes...:
echo "[[\"Graz\",5],[\"Rio de Janeiro\",3],[\"Bräcke\",2],[\"Campinas\",2],[\"Colchester\",2],[\"Cunewalde\",2],[\"Lille\",2],[\"London\",2],[\"Charleroi\",1],[\"Caracas\",1],[\"Butte\",1],[\"Buenos Aires\",1]]" | jq '[.[][1]] | add'
Upvotes: 0
Reputation: 1152
Although it looks that you already got a response, I would suggest a general solution for all possible formats, not only this one that has one-level array nesting, and doesn't even consider objects or whatever. The following code will recursively iterate objects and arrays looking for values:
function isArray(obj) {
return !!(obj.length) && ((obj + "") !== obj);
}
function isObject(obj) {
return obj.toString() === "[object Object]";
}
function getKeys(obj) {
var keys;
if (isArray(obj)) {
keys = [];
for (var i = 0; i < obj.length; i++) {
keys.push(i);
}
} else {
keys = Object.keys(obj)
}
return keys;
}
function deepLength(arr) {
var totalValues = 0;
var keys = getKeys(arr);
for (var i = 0; i < keys.length; i++) {
if (isArray(arr[keys[i]]) || isObject(arr[keys[i]])) {
totalValues += deepLength(arr[keys[i]])
} else {
totalValues++;
}
}
return totalValues;
}
I just didn't invest too much time with the isArray and isObject functions and I would recommend you using maybe those from lodash or underscore, or whatever but definitely not these.
Upvotes: 0
Reputation: 874
Just to complete Jake's excellent answer, I would like to point out that, since the question is tagged with the jquery
tag, another approach could be the use of $.each
:
function sumJSON(str) {
var data = JSON.parse(str),
total = 0;
$.each(data, function(index, value) {
total += value[1];
});
return total;
}
$(document).ready(function() {
var string = '[["Graz", 5.0],["Rio de Janeiro", 3.0],["Bräcke", 2.0],["Campinas", 2.0],["Colchester", 2.0],["Cunewalde", 2.0],["Lille", 2.0],["London", 2.0],["Charleroi", 1.0],["Caracas", 1.0],["Butte", 1.0],["Buenos Aires", 1.0]]';
$(".count").text( sumJSON(string) );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Total: <span class="count"></span>
</p>
Upvotes: 1
Reputation: 330
This is hardly a sexy answer, but it works. Note, since it uses .length, it will only work on an array:
var
jsonArr = [["Graz", 5.0],["Rio de Janeiro", 3.0],["Bräcke",2.0],["Campinas", 2.0],
["Colchester", 2.0],["Cunewalde", 2.0],["Lille", 2.0],["London", 2.0],
["Charleroi", 1.0],["Caracas", 1.0],["Butte", 1.0],["Buenos Aires", 1.0]],
total = 0;
for (var i in jsonArr) {
total += jsonArr[i].length;
}
console.log(total); // 24
Upvotes: 0
Reputation: 2470
Loop through the results and add it up...
var data = JSON.parse('[["Graz", 5.0],["Rio de Janeiro", 3.0],["Bräcke", 2.0],["Campinas", 2.0],["Colchester", 2.0],["Cunewalde", 2.0],["Lille", 2.0],["London", 2.0],["Charleroi", 1.0],["Caracas", 1.0],["Butte", 1.0],["Buenos Aires", 1.0]]');
var total = 0;
for (var i = 0; i < data.length; i++) {
total += data[i][1];
}
Upvotes: 6