Question

how would I reduce bunch of deeply nested arrays and get the biggest value for certain keys? here is my data format:

var data = [
{
"category": "Cat 1",
"subcategories": [
  {
    "subcategory": "Subcate 1 a",
    "problems": [
      {
        "problem": "Problem 1 a 1",
        "total": 3,
        "breakdown": {
          "moderate": 1,
          "severe": 2
        }
      },
      {
        "problem": "Problem 1 a 2",
        "total": 6,
        "breakdown": {
          "moderate": 5,
          "severe": 1
        }
      }
    ]
  }
]
},
{
"category": "Cat 2",
"subcategories": [
  {
    "subcategory": "Subcate 2 a",
    "problems": [
      {
        "problem": "Problem 2 a 1",
        "total": 8,
        "breakdown": {
          "moderate": 5,
          "severe": 3
        }
      }
    ]
  },
  {
    "subcategory": "Subcat 2 b",
    "problems": [
      {
        "problem": "Problem 2 b 1",
        "total": 4,
        "breakdown": {
          "moderate": 1,
          "severe": 3
        }
      },
      {
        "problem": "Problem 2 b 2",
        "total": 2,
        "breakdown": {
          "moderate": 2,
          "severe": 0
        }
      }
    ]
  }
]
}
]

How would I get an array of every value for "moderate" and "severe"? So in this example I would need

[1,2,5,1,5,3,1,3,2,0]

And then I would like to do d3.max(array) to get the biggest value from it.

Was it helpful?

Solution

This is the working fiddle .Just convert the object into array like this:

 var array = $.map(data, function(value, index) {
            return [value];
    });

var arr =[];

for(var key in array)
{
    var subcategories = array[key]['subcategories'];
  for(var j in subcategories)
  {    
      var prob = subcategories[j]['problems'];
      for(var i in prob)
      {
           var moderate = prob[i]['breakdown']['moderate'];
           var severe = prob[i]['breakdown']['severe'];
           arr.push(moderate);
           arr.push(severe);
       }
   }
}
alert(arr); //required array
var max = d3.max(arr) //required max value

and loop through it and save the values into a final array!

OTHER TIPS

This is a little long-winded but it might work:

var array = new Array();
for (var h=0; h<data.length; h++){
    for (var i=0; i<data[h].length; i++){
        for (var j=0; j<data[h].subcategories[i].length; j++){
            for (var k=0; k<data[h].subcategories[i].problems[j].length; k++){
                array.push(data[h].subcategories[i].problems[j].breakdown.moderate);
                array.push(data[h].subcategories[i].problems[j].breakdown.severe);
            }
        }
    }
}
//Then get the max of array

I may have made a mistake somewhere, but you could use this general idea to produce the desired result.

In order to pass deeply nested json use jsonpath jQuery plugin

Fiddle

JS:

var mod=jsonPath(data, '$..subcategories..problems..moderate');
var path=mod.concat(jsonPath(data, '$..subcategories..problems..severe'));
$("#max").html(d3.max(path));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top