简体   繁体   中英

Convert one javascript nested object data structure to nested arrays

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]]];

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);
    }
}

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 .

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/

I had similar problem. My goal was to convert a list of strings into a valid format for http://ivantage.github.io/angular-ivh-treeview/

This was my starting point:

[
  "A\\A1\\Test1",
  "A\\A1\\Test2",
  "A\\A2\\Test3",
  "B\\Test4",
  "B\\Test5",
  "B\\B1\\Test6",
  "B\\B1\\Test7",
  "B\\B1\\Test8",
  "C\\C1\\C1a\\Test9",
  "C\\C1\\C1b\\Test10",
  "C\\C2\\C2a\\Test11",
  "C\\C2\\C2a\\Test12",
  "C\\C2\\C2a\\Test13",
  "C\\C3\\Test14",
  "C\\Test15",
  "C\\Test16"
]

And I needed following format:

[
  {
    "label": "Selected Tests",
    "children": [
      {
        "label": "A",
        "children": [
          {
            "label": "A1",
            "children": [
              {
                "label": "Test1",
                "value": true
              },
              {
                "label": "Test2",
                "value": true
              }
            ]
          },
          {
            "label": "A2",
            "children": [
              {
                "label": "Test3",
                "value": true
              }
            ]
          }
        ]
      }
    ]
  }
]

See my solution https://jsfiddle.net/ydt3gewn/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM