Javascript 如何将对象数组转换为特定格式的对象?

Javascript 如何将对象数组转换为特定格式的对象?,javascript,arrays,object,antd,Javascript,Arrays,Object,Antd,我一直在努力解决一个问题,我正试图以一种更好的方式解决这个问题。基本上我有一个集合,或者更简单地说是一个对象数组。我将粘贴收藏: [ { "pluginId": "fy9h-dKw", "pluginVersion": 1, "pcVersion": "4.26", "platform": "win",

我一直在努力解决一个问题,我正试图以一种更好的方式解决这个问题。基本上我有一个集合,或者更简单地说是一个对象数组。我将粘贴收藏:

[
    {
      "pluginId": "fy9h-dKw",
      "pluginVersion": 1,
      "pcVersion": "4.26",
      "platform": "win",
      "previews": [
        {
          "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
        }
      ],
      "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
    },
    {
      "pluginId": "fy9h-dKw",
      "pluginVersion": 1,
      "pcVersion": "4.26",
      "platform": "mac",
      "previews": [
        {
          "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
        }
      ],
      "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
    },
    {
      "pluginId": "fy9h-dKw",
      "pluginVersion": 1,
      "pcVersion": "4.26",
      "platform": "linux",
      "previews": [
        {
          "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
        }
      ],
      "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
    },
    {
      "pluginId": "fy9h-dKw1",
      "pluginVersion": 1,
      "pcVersion": "4.26",
      "platform": "win",
      "previews": [
        {
          "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
        }
      ],
      "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
    },
    {
      "pluginId": "fy9h-dKw1",
      "pluginVersion": 1,
      "pcVersion": "4.26",
      "platform": "mac",
      "previews": [
        {
          "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
        }
      ],
      "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
    },
    {
      "pluginId": "fy9h-dKw1",
      "pluginVersion": 1,
      "pcVersion": "4.26",
      "platform": "linux",
      "previews": [
        {
          "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
        }
      ],
      "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
    },
    {
      "pluginId": "fy9h-dKw2",
      "pluginVersion": 1,
      "pcVersion": "4.26",
      "platform": "win",
      "previews": [
        {
          "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
        }
      ],
      "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
    }
  ]
现在,我想将其更改为特定格式的原因是,我可以将其与一个UI库一起使用,该UI库指定它为特定格式。我将粘贴最终结果:

[
{
    "name": "fy9h-dKw", "children": [
        {
            "name": "4.26", "children": [
                {
                    "name": "1", "children": [
                        {
                            "name": "mac", "children": [
                                {
                                    "pluginId": "fy9h-dKw",
                                    "pluginVersion": 1,
                                    "pcVersion": "4.26",
                                    "platform": "mac",
                                    "previews": [
                                        {
                                            "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
                                        }
                                    ],
                                    "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
                                }
                            ],
                            "name": "windows", "children": [
                                {
                                    "pluginId": "fy9h-dKw",
                                    "pluginVersion": 1,
                                    "pcVersion": "4.26",
                                    "platform": "win",
                                    "previews": [
                                        {
                                            "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
                                        }
                                    ],
                                    "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
                                }
                            ],
                            "name": "linux", "children": [
                                {
                                    "pluginId": "fy9h-dKw",
                                    "pluginVersion": 1,
                                    "pcVersion": "4.26",
                                    "platform": "linux",
                                    "previews": [
                                        {
                                            "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
                                        }
                                    ],
                                    "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
                                }
                            ],
                        }
                    ]
                }
            ]
        }
    ]
},
{
    "name": "fy9h-dKw1", "children": [
        {
            "name": "4.26", "children": [
                {
                    "name": "1", "children": [
                        {
                            "name": "mac", "children": [
                                {
                                    "pluginId": "fy9h-dKw1",
                                    "pluginVersion": 1,
                                    "pcVersion": "4.26",
                                    "platform": "mac",
                                    "previews": [
                                        {
                                            "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
                                        }
                                    ],
                                    "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
                                }
                            ],
                            "name": "windows", "children": [
                                {
                                    "pluginId": "fy9h-dKw1",
                                    "pluginVersion": 1,
                                    "pcVersion": "4.26",
                                    "platform": "win",
                                    "previews": [
                                        {
                                            "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
                                        }
                                    ],
                                    "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
                                },

                            ],
                            "name": "linux", "children": [
                                {
                                    "pluginId": "fy9h-dKw1",
                                    "pluginVersion": 1,
                                    "pcVersion": "4.26",
                                    "platform": "linux",
                                    "previews": [
                                        {
                                            "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
                                        }
                                    ],
                                    "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
                                }
                            ],
                        }
                    ]
                }
            ]
        }
    ]
},
{
    "name": "fy9h-dKw2", "children": [
        {
            "name": "4.26", "children": [
                {
                    "name": "1", "children": [
                        {
                            "name": "mac", "children": [
                                {
                                }
                            ],
                            "name": "windows", "children": [
                                {
                                    "pluginId": "fy9h-dKw2",
                                    "pluginVersion": 1,
                                    "pcVersion": "4.26",
                                    "platform": "win",
                                    "previews": [
                                        {
                                            "uri": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg"
                                        }
                                    ],
                                    "pluginUrl": "/e2d4-dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip"
                                }
                            ],
                            "name": "linux", "children": [
                                {
                                }
                            ],
                        }
                    ]
                }
            ]
        }
    ]
}
]
为了消除这一最终结果的混乱,我只想按照他们的插件ID,然后按照他们的pcVersion,然后按照他们的插件反转,最后按照他们的平台对他们进行分组。我将只粘贴最终结果的格式:

[
{
    "name": "pluginId", "children": [
        {
            "name": "pcVersion", "children": [
                {
                    "name": "pluginVersion", "children": [
                        {
                            "name": "mac", "children": [
                                {
                                }
                            ],
                            "name": "windows", "children": [
                                {
                                }
                            ],
                            "name": "linux", "children": [
                                {
                                }
                            ],
                        }
                    ]
                }
            ]
        }
    ]
}
]
我发现这个方法很好。但是我必须为它写很多糟糕的循环,而且它太复杂了


我非常感谢任何提示或线索来解决这个问题,因为我已经被困在这个问题上好几个小时了,而且非常沮丧。提前感谢。

这里是一个相当紧凑的示例,使用嵌套的
reduce()
调用并将描述层次结构的键数组传递给内部
reduce()
。这允许您通过传递不同的层次结构数组,以多种方式重构原始数组

const srcArr=[{“pluginId”:“fy9h dKw”,“pluginreversion”:1,“pcVersion”:“4.26”,“platform”:“win”,“previews”:[{“uri”:“/e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg”}],“pluginUrl”:“pce2d4dkv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip”;“pluginId”;“fy9h”,“pluginUrl”:“pluginUrl”:“pce2dqv/4.26”平台”:“mac”、“预览”:“{”uri:“/e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg”],“pluginUrl:“/e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip”,“{”pluginId:“fy9h dKw”、“pluginVersion”:1,“pcVersion”4.26”、“平台“:“linux”、“预览”:“{”uri:“e2d4 dKv/4.26/7f4bc5e9-75E75E3D”3.3-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-4-5-5-5-5-5-5-5-7-7-5-5-7-7-5-7-5-5-5-5-5-5-5-5-5-5-5-7-7-7-7-7-8-7-7-7-7-7-7-7-8-8-7-7-8-7-8-8-8-8-8-8-7-8-7-8-7-8-8-8-8-8-8-8-8-8-4-4-4-4-4-4-4-4-7-4-4-4-7-7-7-7-7-7-8-7-7-8-8-7-7-7-7-7-7-7-7-7-8 D-dKw1“,”pluginVersion“:1,“pcVersion:”4.26“,”平台“:”mac“,”预览“,”e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg“,”pluginUrl“:”/e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip“,”pluginId:”fy9h-DK“,”反转“:”1,“pcVersion”4.26“,”linux“平台“,”预览“:”“/e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.jpg”,“pluginUrl”:/e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip,{“pluginId”:“fy9h-dKw2”,“plugin反转”:1,“pcVersion”:“4.26”,“平台”:“win”,“预览”:“{“uri”:“/E2D4V/4.26/1/75792AE753D”/e2d4 dKv/4.26/1/7f4bc5e9-3721-4f83-abd4-75792ae75e3d.zip“}]
函数重构数组(arr,树){
返回arr.reduce((a,o)=>{
常量内部=树。减少((ka,k)=>{
const match=ka.find(e=>e.name==o[k]);
如果(匹配){
ka=匹配。儿童;
}否则{
常量n={name:o[k],子项:[]};
ka.推力(n);
ka=n.儿童;
}
返回ka;
},a);
inner.push({…o});
返回a;
}, []);
}
常数树1=[
“pluginId”,
“pcVersion”,
"倒转",,
“平台”];
log(重构数组(srcArr,tree1));
常数树2=[
"平台",,
'插入反转'];

log(重构数组(srcArr,tree2))
非常感谢@pilchard。它以如此优雅的姿态满足了我的所有需求。你是一位圣人。现在我有点难以理解它,因为reduce是一个新概念,但它的简洁性让我大吃一惊。我希望你不会介意我在评论部分就它打扰你几次。干杯!!!很高兴它对你有用。欢迎光临提出问题,我很乐意帮忙。