Convert one javascript nested object data structure to nested arrays-Collection of common programming errors

I am trying to convert a JSON string in a Javascript object literal. I think it is possible with some loops, but i couldn’t get it done. The target structure is shown below, “chartData”.

Fiddle can be found here: http://jsbin.com/ajemih/13/edit

Here’s the JSON data:

{
   "1b":{
      "allLoad":"130",
      "loadMovement":"111",
      "allMovement":"111"
   },
   "1a":{
      "allLoad":"910",
      "loadMovement":"671",
      "allMovement":"280"
   },
   "systemLoad":"963"
}

This should it look like after the conversion:

chartData = [[['loadMovement', 111], 
              ['allMovement', 120], 
              ['allLoad', 130]], 
             [['Load+Move', 671], 
              ['allMovement', 280], 
              ['allLoad', 910]]];
  1. I think this would work:

    Working demo: http://jsfiddle.net/jfriend00/YmjDR/

    var data = {
       "1b":{
          "allLoad":"130",
          "loadMovement":"111",
          "allMovement":"111"
       },
       "1a":{
          "allLoad":"910",
          "loadMovement":"671",
          "allMovement":"280"
       },
       "systemLoad":"963"
    };
    
    var chartData = [];
    
    for (var i in data) {
        var item = data[i];
        var outer = [];
        // skip over items in the outer object that aren't nested objects themselves
        if (typeof item === "object") {
            for (var j in item) {
                var temp = [];
                temp.push(j);
                temp.push(item[j]);
                outer.push(temp);
            }
        }
        if (outer.length) {
            chartData.push(outer);
        }
    }
    
  2. You could do something like this:

    var chartData = []
    
    for(var key in data) {        
        var properties = data[key];
    
        if(typeof properties === "object") {
           var array = [];
    
           for(var propKey in properties) {
               array.push([propKey, properties[propKey]])
           }
    
           chartData.push(array);
        }             
    }
    

    Check out the fiddle.

  3. You need to map the data manually. Thats actually more a diligent but routine piece of work.

    var jsonData = 'your json string';
    
    Object.keys( jsonData ).map(function( key ) {
        if( typeof jsonData[ key ] === 'object' ) {
            return Object.keys( jsonData[ key ] ).sort(function( a, b ) {
                return +jsonData[ key ][ a ] - +jsonData[ key ][ b ];
            }).map(function( name ) {
                return [ name, jsonData[ key ][ name ] ];
            });
        }
    }).filter( Boolean );
    

    The above code will sort each group by its numeric value and then map a new array in the required style. Since .map() possibly returns undefined values on non-object elements, we need to filter those out before or afterwards.

    See http://jsfiddle.net/WjZB2/2/

Originally posted 2013-11-09 23:33:40.