简体   繁体   English

如何在JavaScript中将多维对象转换为表格行列格式

[英]How can I convert multidimensional object to table row column format in javascript

I have got a multidimensional object in my ajax response. 我的ajax响应中有一个多维对象。 Here is the object 这是对象

 response = {
             "Bangladesh": {
                 "2016": 5,
                 "2017": 1
            },
            "Nepal": {
                "2016": 1,
                "2019": 10
            }
    };

Now I want to rearrange that array to a google chart Data table format as bellow 现在我想将该数组重新排列为谷歌图表数据表格式,如下

['Year', 'banglladesh', 'nepal' ],
['2016', 5,1],
['2017',  1,0],
['2019',  0, 10],

That was hard, I'm sure it can be improved. 那很难,我相信它可以改善。 see comments 看评论

 const response = { "Bangladesh": { "2016": 5, "2017": 1 }, "Nepal": { "2016": 1, "2019": 10 } }; const parse = val => isNaN(val) ? val : Number(val) const tablerize = (obj, unique) => { const columns = Object.keys(obj) const table = [[unique, ...columns]] // indexed by the unique key const indexed = {} // sort by the index columns.forEach((key, ii) => { return Object.keys(obj[key]).forEach((prop) => { if (!indexed[prop]) { indexed[prop] = {} } indexed[prop][ii] = obj[key][prop] }) }) // add to the output table Object.keys(indexed).forEach(key => { table.push([ parse(key), // return the value at the key index ...columns.map((k, ii) => parse(indexed[key][ii]) || 0) ]) }) return table } console.log( tablerize(response, 'Year') ) 
 <script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script> 

Not sure what the third item in the numeric arrays are coming from based on the response provided, but at least this will get you pointed in the right direction: 不确定基于提供的响应,数字数组中的第三项来自何处,但是至少这会使您指向正确的方向:

var response = {
  "Bangladesh": {
        "2016": 5,
        "2017": 1
   },
   "Nepal": {
       "2016": 1,
       "2019": 10
   }
};

var out = [];

var header = ["Year"];
out.push(header);

for(var prop in response){
    header.push(prop);
    var item = response[prop];
    for(var year in item){
        out.push([year, item[year] ] );
    }
}

console.log(out);

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

相关问题 如何将JavaScript对象(JSON)转换为JSV格式? - How can I convert a JavaScript object(JSON) into JSV format? 如何将表格行数据转换为 javascript 中的列数据 - How can I convert tabular row data to column data in javascript 如何在Javascript中将列中的字符串转换为行 - How can i convert a string in a column to row in Javascript 如何将该字符串转换为多维JavaScript数组 - How can I convert this string into a multidimensional JavaScript array 如何将JavaScript变量中保存的_User对象转换回其格式,可以将其保存到parse.com指针列中 - How to convert a _User object held in JavaScript variable back to format it can be saved into a parse.com pointer column 如何使用按行到列选择的表格复选框写入 json 格式? - How can I write to json format with the table checkbox I selected by row to column? 如何从多维数组javascript中删除对象? - How can I remove object from multidimensional array javascript? 如何使用JavaScript中的唯一键访问多维对象中的值? - How can I access a value in a multidimensional object with unique keys in Javascript? 如何使用 javascript 将 JSON 列对象转换为 CSV 格式 - How to convert JSON column object to CSV format using javascript 如何将具有 json 格式数据的 excel 列转换为 javascript object? - How to convert excel column with json format data to a javascript object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM