【问题标题】:How to translate object data to a format known by react-d3-tree如何将对象数据转换为 react-d3-tree 已知的格式
【发布时间】:2019-07-05 03:29:40
【问题描述】:

我目前在 react-d3-tree 中使用我的数据时遇到问题。来自服务器的数据与 react-d3-tree 接受的格式不兼容。

有人告诉我,对每个​​项目使用 foreach 函数来处理这个过程会起作用,但我认为这会很慢,尤其是当数据太大时。

现在的数据(如我的控制台所示)是这样的:

0:  {
        series: 'Fate'
        type: 'Servant'
        class: 'Saber',
    },
1:  {
        series: 'Fate',
        type: 'Servant'
        class: 'Archer',
    },
2:  {
        series: 'Fate',
        type: 'Demi-servant'
        class: 'Shielder',
    }

而我想实现这个结构:

[
    {
        name: 'Fate',
        children: [
            {
                name: 'Servant',
                children: [
                    {
                        name: 'Saber',
                        children: []
                    },
                    {
                        name: 'Archer',
                        children: []
                    }
                ]
            },
            {
                name: 'Demi-servant',
                children: [
                    {
                        name: 'Shielder',
                        children: []
                    }
                ]
            }
        ]
    }
]

这只是示例数据,稍后我可能会转换具有更多孩子的数据。是否有任何 npm 包可以提供帮助?

【问题讨论】:

    标签: javascript reactjs react-d3


    【解决方案1】:

    没关系,我真的找到了答案。

    如果有人也发现这个问题,我会分享这些有用的链接。

    https://www.npmjs.com/package/shape-json
    https://www.npmjs.com/package/shape-array

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多