(forgive me if I use slightly incorrect language – feel free to constructively correct as needed)
There are a couple posts about getting data from JSON data of siblings in the returned object, but I’m having trouble applying that information to my situation:
I have a bunch of objects that are getting returned as JSON from a REST call and for each object with a node of a certain key:value I need to extract the numeric value of a sibling node of a specific key. For example:
For the following list of objects, I need to add up the numbers in “file_size” for each object with matching “desc” and return that to matching input values on the page.
{"ResultSet":{
Result":[
{
"file_size":"722694",
"desc":"description1",
"format":"GIF"
},
{
"file_size":"19754932",
"desc":"description1",
"format":"JPEG"
},
{
"file_size":"778174",
"desc":"description2",
"format":"GIF"
},
{
"file_size":"244569996",
"desc":"description1",
"format":"PNG"
},
{
"file_size":"466918",
"desc":"description2",
"format":"TIFF"
}
]
}}
You can use the following function:
And call it like this:
To display an alert with the summation of all
"description1"file sizes.A working JSFiddle is here: http://jsfiddle.net/Q9n2U/.
In response to your updates and comments, here is some new code that creates some
divs with the summations for all descriptions. I took out thehasOwnPropertycode because you changed your data set, but note that if you have objects in the data array without thefile_sizeproperty, you must usehasOwnPropertyto check for it. You should be able to adjust this for your jQuery.eachfairly easily.A working JSFiddle is here: http://jsfiddle.net/DxCLu/.