【问题标题】:how to extract keys of nested object in same nested format added by new key to keys如何以新键添加到键的相同嵌套格式提取嵌套对象的键
【发布时间】:2018-08-30 20:44:21
【问题描述】:

我正在使用react table,我需要在其中提供我的 api 数据键,并添加新的键 Header 来创建 React Table Head (TH > TD);

以下是从 API 获取的示例数据,我需要将对象键传递给表头,将对象键传递给表体。表头需要为常用键保留。

To Achieve this

我的数据样本:

{
  'data':
    {
        'id': 486897532,
        'active': null,
        'acc_info': [
        {
          'acc_name': 'saving',
          'bank_info': [
            {
              'bank_name': 'ABC',
              'bank_address': 'address',
            },
            {
              'bank_name': 'DEF',
              'bank_address': 'address',
            }]
        },
        {
            'acc_name': 'FXE',
            'bank_info': [
            {
                'bank_name': 'ABC',
                'bank_address': 'address',
            },
            {
                'bank_name': 'DEF',
                'bank_address': 'address',
            }]
        },
      ],
    },
    {
        'id': 56465945,
        'active': null,
        'acc_info': [
        {
          'acc_name': 'current',
          'bank_info': [
            {
              'bank_name': 'ABC',
              'bank_address': 'address',
            },
            {
              'bank_name': 'DEF',
              'bank_address': 'address',
            }]
        },
        {
            'acc_name': 'FXE',
            'bank_info': [
            {
                'bank_name': 'ABC',
                'bank_address': 'address',
            },
            {
                'bank_name': 'DEF',
                'bank_address': 'address',
            }]
        },
      ],
    }
}

预期结果应该是:

[
  {
    Header: "id"
  },
  {
    Header: "active"
  },
  {
    Header: "acc_info",
    columns: [
    {
       Header: "acc_name",
    },
    {
       Header: "bank_name",
    },
    {
       Header: "bank_address"
    }]
 }
]

【问题讨论】:

    标签: javascript reactjs react-table


    【解决方案1】:

    问题已解决!!

    那些正在使用或刚接触 React Table 的步骤如下:

    表格标题

    1. React Table 带有 'columns' 道具,它需要对象 标头和访问键。 TH 标题的标题和访问器 访问关注表体列
    2. 使用 js Map 或 Ladash_.map 获取期望值
    3. 使用 'Header' 和 'accessor' 键创建对象变量,并将数据映射为 价值观

    干杯..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      相关资源
      最近更新 更多