简体   繁体   English

从json对象中共享数据并创建层次结构

[英]aggretaing data from json objects and creating hierarchies

I have to create a tree structured table with some data given in the form of json array. 我必须创建一个树形结构表,其中一些数据以json数组的形式给出。

let us take an example array 让我们举一个示例数组

array :
[
{ "continent": "Asia", "country":"India", "population": 100, "GDP":4},
{ "continent": "Asia", "country":"china", "population": 130, "GDP":20},
{ "continent": "Asia", "country":"Pakistan", "population": 20, "GDP":1},
{ "continent": "Europe", "country":"UK", "population": 2, "GDP":15},
{ "continent": "Europe", "country":"France", "population": 4, "GDP":16},
{ "continent": "Europe", "country":"Germany", "population": 5, "GDP":21}
]

the table is like a tree which can be expanded or collapsed. 该表就像一棵树,可以展开或折叠。 let the tree be like, 让树像,

    continent          country         population       GDP

    Asia           +                   250               25

    Europe         -                   11                52
                       UK              2                 15
                       France          4                 16
                       Germany         5                 21

on the above table we expanded Europe, in the below table we expand Asia. 在上表中我们扩展了欧洲,在下表中我们扩展了亚洲。

    continent           country            population       GDP

    Asia           -                       250              25
                        India              100              4
                        China              130              20
                        Pakisthan          20               1

    Europe       +                         11               52

please help me in creating the table with aggregated values. 请帮我创建具有聚合值的表格。

Thanks :) 谢谢 :)

You mention javascript in your tags, so I assume you need the answer in JS. 你在标签中提到了javascript ,所以我假设你需要JS中的答案。 I'm giving you keywords that you'll need to study up on. 我给你提供了你需要学习的关键词。

First, declare the variable. 首先,声明变量。

var regions = [
  { "continent": "Asia", "country":"India", "population": 100, "GDP":4},
  { "continent": "Asia", "country":"china", "population": 130, "GDP":20},
  { "continent": "Asia", "country":"Pakistan", "population": 20, "GDP":1},
  { "continent": "Europe", "country":"UK", "population": 2, "GDP":15},
  { "continent": "Europe", "country":"France", "population": 4, "GDP":16},
  { "continent": "Europe", "country":"Germany", "population": 5, "GDP":21}
];

Then, you need something to hold the various answers: 然后,你需要一些东西来保持各种答案:

var results = {}; var results = {}; // Note, this is an object. //注意,这是一个对象。 Not an array. 不是数组。

Next, loop over the array with standard loop processing. 接下来,使用标准循环处理循环遍历数组。 A for loop if you need to support older browsers, .forEach if you can only target newer ones. 如果您需要支持旧版浏览器, .forEach for循环,如果您只能定位较新的浏览器, .forEach

With each loop, see if continent exists in results . 对于每个循环,查看results是否存在continent If not, add the property with a value of 0. 如果不是,请添加值为0的property

Then just accumulate the GDP in results[<continent>] 然后只是在results[<continent>]积累GDP

 function showHide(){ var td = this, tr = this.parentElement, next = td.nextElementSibling; if(tr.className == 'visible'){ var rowSpan = td.rowSpan; td.oldRowSpan = rowSpan; td.rowSpan = next.rowSpan = 1; tr.className = 'collapsed'; for(var i=1; i<rowSpan; ++i){ tr = tr.nextElementSibling; tr.className = 'hidden'; } }else{ td.rowSpan = next.rowSpan = td.oldRowSpan; rowSpan = td.oldRowSpan; tr.className = 'visible'; for(var i=1; i<rowSpan; ++i){ tr = tr.nextElementSibling; tr.className = ''; } } } var data = [ { "continent": "Asia", "country":"India", "population": 100, "GDP":4}, { "continent": "Asia", "country":"china", "population": 130, "GDP":20}, { "continent": "Asia", "country":"Pakistan", "population": 20, "GDP":1}, { "continent": "Europe", "country":"UK", "population": 2, "GDP":15}, { "continent": "Europe", "country":"France", "population": 4, "GDP":16}, { "continent": "Europe", "country":"Germany", "population": 5, "GDP":21} ], obj = {}; for(var i=0; i<data.length; ++i) { var continent = data[i].continent, arr = obj[continent] = obj[continent] || []; arr.push([data[i].country,data[i].population,data[i].GDP]); } var table = document.createElement('table'), thead = document.createElement('thead'), tbody = document.createElement('tbody'), tr = document.createElement('tr'); var keys = ['','Continent','Country','Population','GDP']; for(var i=0; i<keys.length; ++i){ var td = document.createElement('td'); td.appendChild(document.createTextNode(keys[i])); tr.appendChild(td); } thead.appendChild(tr); table.appendChild(thead); for(var i in obj) { var dataArr = []; tr = document.createElement('tr'), td = document.createElement('td'); td.rowSpan = obj[i].length + 1; td.onclick = showHide; tr.appendChild(td); td = document.createElement('td'); td.appendChild(document.createTextNode(i)); td.rowSpan = obj[i].length + 1; tr.appendChild(td); tbody.appendChild(tr); for(var j=0; j<obj[i].length; ++j){ var subTr = document.createElement('tr'); for(var k=0; k<obj[i][j].length; ++k){ td = document.createElement('td'); if(k>0) dataArr[k] = (dataArr[k] || 0) + obj[i][j][k]; td.appendChild(document.createTextNode(obj[i][j][k])); subTr.appendChild(td); } tbody.appendChild(subTr); } for(var j=0; j<dataArr.length; ++j){ td = document.createElement('td') td.className = 'data-sum'; if(dataArr[j]) td.appendChild(document.createTextNode(dataArr[j])); tr.appendChild(td); } tr.className = 'visible'; } table.appendChild(tbody); document.body.appendChild(table); 
 table { border-collapse: collapse; } thead { font-weight: bold; font-size: 120% } td { border: 2px solid black; } .hidden, .visible > .data-sum { display: none; } .collapsed > :first-child, .visible > :first-child { cursor: pointer; } .collapsed > :first-child:before { content: '[+]'; font-family: monospace; } .visible > :first-child:before { content: '[-]'; font-family: monospace; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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