简体   繁体   English

将键、值 JSON 数组转换为表格 JSON 格式

[英]Convert key, values JSON array to tabular JSON format

I have following JSON data for Chart我有以下 JSON 图表数据

var chartJson = [
    {
        header : '2016',
        values : [1, 5, 9]
    },
    {
        header : '2017',
        values : [2, 4, 8]
    },
    {
        header : '2018',
        values : [3, 1, 5]
    }
];

And needs to convert it into this format to feed my HTML table并需要将其转换成这种格式以提供我的 HTML 表

var tableJson = [
    {
        2016 : 1,
        2017 : 2,
        2018 : 3
    },
    {
        2016 : 5,
        2017 : 4,
        2018 : 1
    },
    {
        2016 : 9,
        2017 : 8,
        2018 : 5
    }
];

Any quick help will be appreciated to convert it into this format.任何快速帮助将其转换为这种格式将不胜感激。 I tried using this code, but somehow missing on the logic.我尝试使用此代码,但不知何故缺少逻辑。

let table = [];
    for(var row of chartJson ){
        for(var value of row.values)
        {
            table.push(
                {
                    column : row.header,
                    value : value
                });
        }
    }

 var chartJson = [{ header: '2016', values: [1, 5, 9] }, { header: '2017', values: [2, 4, 8] }, { header: '2018', values: [3, 1, 5] } ]; let table = []; chartJson.forEach((row, index) => { row.values.forEach((val, j) => { table[j] = {...table[j], [row.header]: val } }); }); console.log(table)

  1. Iterate through every chartJson 's element with its' values(through inner loop) till values' length and make an object from that.遍历每个chartJson的元素及其值(通过内部循环)直到values'长度,并从中生成 object。
  2. Finally, push that object into the table array.最后,将 object 推入table数组。

That's it.而已。

Have a look at the snippet below:看看下面的片段:

 var chartJson = [ { header: '2016', values: [1, 5, 9] }, { header: '2017', values: [2, 4, 8] }, { header: '2018', values: [3, 1, 5] } ]; let table = []; let len_of_chartJson = chartJson.length, len_of_values = chartJson[0].values.length; for (var i = 0; i < len_of_chartJson; i++) { let obj = {}; for (var j = 0; j < len_of_values; j++) { obj[chartJson[j].header] = chartJson[j].values[i]; } table.push(obj); } console.log(table);

let table = chartJson.reduce((tbl, rec) => {
          rec.values.forEach((num, index) => {
                  if(!tbl[index]){
                          tbl[index] = {}
                  }
                  tbl[index][rec.header] = num
          })
          return tbl
          }, [])

Array reduce function is used to loop through each object, than for each object it loop through each value, checking if the index exist in the table, if it does not exist, it create an empty object at current index.数组 reduce function 用于循环遍历每个 object,而不是每个 object 循环遍历每个值,检查索引是否存在于表中,如果不存在,则在当前索引处创建一个空的 object。 Finally it creates a key value in the current index object.最后它在当前索引 object 中创建一个键值。

You read more about reduce function below https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce你阅读更多关于 reduce function below https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

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

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