【问题标题】:Convert rows to column from javascript array从javascript数组将行转换为列
【发布时间】:2018-03-23 19:39:55
【问题描述】:

我正在研究 ReactJS,想知道是否有办法将 javascript 数组从行转换为列。

[
    { study: 'KOOS', date: '05/06/2005', question: 'Standing upright', answer: 'Moderate' },
    { study: 'KOOS', date: '05/06/2006', question: 'Standing upright', answer: 'Severe' },
    { study: 'KOOS', date: '05/06/2007', question: 'Standing upright', answer: 'Extreme' },
    { study: 'KOOS', date: '05/06/2008', question: 'Standing upright', answer: 'Severe' },
    { study: 'KOOS', date: '05/06/2005', question: 'Going up or down stairs', answer: 'Extreme' },
    { study: 'KOOS', date: '05/06/2006', question: 'Going up or down stairs', answer: 'Moderate' },
    { study: 'KOOS', date: '05/06/2007', question: 'Going up or down stairs', answer: 'Extreme' },
    { study: 'KOOS', date: '05/06/2008', question: 'Going up or down stairs', answer: 'Moderate' }
]

我想把它转换成这样的 html table/div 数据:

Study: Koos
Question                | 05/06/2005 | 05/06/2006 | 05/06/2007 | 05/06/2008 
Standing upright        | Moderate   | Severe     | Extreme    | Severe
Going up or down stairs | Extreme    | Moderate   | Extreme    | Moderate

我发现了这个优秀的库 - json-to-pivot-json。这是我想使用的东西,但唯一的问题是聚合值。

我在 sql 中看到了很多示例,但在 javascript 中找不到类似的示例。

为了帮助其他人,我正在添加完整的 JSX 代码以显示 Rafael 建议的结果。

var output = coll2tbl(surveyResult, "question", "date", "answer"); 

const mappedCells = output.cells.map((row, index) => {
    row.unshift(output.row_headers[index]);
    return row;
})

<table>
    <thead>
        <th>Question</th>
        {
            output.col_headers.map(c => {
                return (
                    <th>{c}</th>
                );
            })                                    
        }   
    </thead>
    <tbody>
    {
        mappedCells.map(row => (
            <tr>{ row.map(cell => (<td>{cell}</td>))}</tr>
        ))
    }                   
    </tbody>
</table>

【问题讨论】:

  • 如果您需要聚合现有的对象数组,然后从中构建一个新对象。

标签: javascript json reactjs


【解决方案1】:

收集到表就绪数据结构

将集合和适当的属性描述符传递给coll2tbl 函数,它将输出一个可用于表的数据结构:

var data = [
    { study: 'KOOS', date: '05/06/2005', question: 'Standing upright', answer: 'Moderate' },
    { study: 'KOOS', date: '05/06/2006', question: 'Standing upright', answer: 'Severe' },
    { study: 'KOOS', date: '05/06/2007', question: 'Standing upright', answer: 'Extreme' },
    { study: 'KOOS', date: '05/06/2008', question: 'Standing upright', answer: 'Severe' },
    { study: 'KOOS', date: '05/06/2005', question: 'Going up or down stairs', answer: 'Extreme' },
    { study: 'KOOS', date: '05/06/2006', question: 'Going up or down stairs', answer: 'Moderate' },
    { study: 'KOOS', date: '05/06/2007', question: 'Going up or down stairs', answer: 'Extreme' },
    { study: 'KOOS', date: '05/06/2008', question: 'Going up or down stairs', answer: 'Moderate' }
];

function get_prop(obj, prop) {
    return prop.split('.').reduce((o,k) => obj[k], obj);
}

function coll2tbl(coll, row_header, col_header, cell) {
    var table = {};
    var row_headers = [];
    var cols = {};

    coll.forEach(a => {
        var h = get_prop(a, row_header);
        if (h in table === false) {
            table[h] = {};
            row_headers.push(h);
        }
        var c = get_prop(a, col_header);
        cols[c] = null;
        table[h][c] = get_prop(a, cell);
    });

    var cells = [];
    for (var row in table)
        cells.push(Object.values(table[row]));

    return { row_headers, col_headers: Object.keys(cols), cells };
}

var table = coll2tbl(data, 'question', 'date', 'answer');
console.log(table);

输出:

{ row_headers: [ 'Standing upright', 'Going up or down stairs' ],
  col_headers: [ '05/06/2005', '05/06/2006', '05/06/2007', '05/06/2008' ],
  cells: 
   [ [ 'Moderate', 'Severe', 'Extreme', 'Severe' ],
     [ 'Extreme', 'Moderate', 'Extreme', 'Moderate' ] ] }

制作标题

  1. thead 的开头添加一个静态th,这是您的Question 字符串所在的位置。
  2. 在 col_headers 中为每个 ch 添加一个 th

制作主体

  1. 迭代coll2tbl()返回的cells
    1. 每个父迭代,创建一个新的tr
    2. 通过使用父迭代器的计数器抓取相应的行标题 (table.row_headers[i]) 添加行标题单元格。
    3. 内部循环将创建单元格,td, (table.cells[i][j])。

理想情况下,您将制作一个采用此数据结构并为您创建 html 的 react 组件。

【讨论】:

猜你喜欢
  • 2021-06-08
  • 2020-04-30
  • 2014-07-13
  • 2011-09-23
  • 2014-01-29
  • 1970-01-01
  • 2014-02-08
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多