{"id":1163,"date":"2022-08-30T15:13:26","date_gmt":"2022-08-30T15:13:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/convert-one-javascript-nested-object-data-structure-to-nested-arrays-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:26","modified_gmt":"2022-08-30T15:13:26","slug":"convert-one-javascript-nested-object-data-structure-to-nested-arrays-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/convert-one-javascript-nested-object-data-structure-to-nested-arrays-collection-of-common-programming-errors\/","title":{"rendered":"Convert one javascript nested object data structure to nested arrays-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to convert a JSON string in a Javascript object literal. I think it is possible with some loops, but i couldn&#8217;t get it done. The target structure is shown below, &#8220;chartData&#8221;.<\/p>\n<p>Fiddle can be found here: http:\/\/jsbin.com\/ajemih\/13\/edit<\/p>\n<p>Here&#8217;s the JSON data:<\/p>\n<pre><code>{\n   \"1b\":{\n      \"allLoad\":\"130\",\n      \"loadMovement\":\"111\",\n      \"allMovement\":\"111\"\n   },\n   \"1a\":{\n      \"allLoad\":\"910\",\n      \"loadMovement\":\"671\",\n      \"allMovement\":\"280\"\n   },\n   \"systemLoad\":\"963\"\n}\n<\/code><\/pre>\n<p>This should it look like after the conversion:<\/p>\n<pre><code>chartData = [[['loadMovement', 111], \n              ['allMovement', 120], \n              ['allLoad', 130]], \n             [['Load+Move', 671], \n              ['allMovement', 280], \n              ['allLoad', 910]]];\n<\/code><\/pre>\n<ol>\n<li>\n<p>I think this would work:<\/p>\n<p>Working demo: http:\/\/jsfiddle.net\/jfriend00\/YmjDR\/<\/p>\n<pre><code>var data = {\n   \"1b\":{\n      \"allLoad\":\"130\",\n      \"loadMovement\":\"111\",\n      \"allMovement\":\"111\"\n   },\n   \"1a\":{\n      \"allLoad\":\"910\",\n      \"loadMovement\":\"671\",\n      \"allMovement\":\"280\"\n   },\n   \"systemLoad\":\"963\"\n};\n\nvar chartData = [];\n\nfor (var i in data) {\n    var item = data[i];\n    var outer = [];\n    \/\/ skip over items in the outer object that aren't nested objects themselves\n    if (typeof item === \"object\") {\n        for (var j in item) {\n            var temp = [];\n            temp.push(j);\n            temp.push(item[j]);\n            outer.push(temp);\n        }\n    }\n    if (outer.length) {\n        chartData.push(outer);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You could do something like this:<\/p>\n<pre><code>var chartData = []\n\nfor(var key in data) {        \n    var properties = data[key];\n\n    if(typeof properties === \"object\") {\n       var array = [];\n\n       for(var propKey in properties) {\n           array.push([propKey, properties[propKey]])\n       }\n\n       chartData.push(array);\n    }             \n}\n<\/code><\/pre>\n<p>Check out the fiddle.<\/p>\n<\/li>\n<li>\n<p>You need to map the data manually. Thats actually more a diligent but routine piece of work.<\/p>\n<pre><code>var jsonData = 'your json string';\n\nObject.keys( jsonData ).map(function( key ) {\n    if( typeof jsonData[ key ] === 'object' ) {\n        return Object.keys( jsonData[ key ] ).sort(function( a, b ) {\n            return +jsonData[ key ][ a ] - +jsonData[ key ][ b ];\n        }).map(function( name ) {\n            return [ name, jsonData[ key ][ name ] ];\n        });\n    }\n}).filter( Boolean );\n<\/code><\/pre>\n<p>The above code will sort each group by its numeric value and then map a new array in the required style. Since <code>.map()<\/code> possibly returns <code>undefined<\/code> values on non-object elements, we need to filter those out before or afterwards.<\/p>\n<p>See http:\/\/jsfiddle.net\/WjZB2\/2\/<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:33:40. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to convert a JSON string in a Javascript object literal. I think it is possible with some loops, but i couldn&#8217;t get it done. The target structure is shown below, &#8220;chartData&#8221;. Fiddle can be found here: http:\/\/jsbin.com\/ajemih\/13\/edit Here&#8217;s the JSON data: { &#8220;1b&#8221;:{ &#8220;allLoad&#8221;:&#8221;130&#8243;, &#8220;loadMovement&#8221;:&#8221;111&#8243;, &#8220;allMovement&#8221;:&#8221;111&#8243; }, &#8220;1a&#8221;:{ &#8220;allLoad&#8221;:&#8221;910&#8243;, &#8220;loadMovement&#8221;:&#8221;671&#8243;, &#8220;allMovement&#8221;:&#8221;280&#8243; }, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1163","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1163"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1163\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}