簡體   English   中英

如何將對象數組轉換為特定格式的對象?

[英]How to convert an array of objects into an object of specific format?

我一直在努力解決一個問題,我正試圖以一種高性能和更好的方式解決這個問題。 基本上我有一個集合,或者更簡單地說是一個對象數組。 我將把收藏貼下來:

[
    {
      "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 庫一起使用。 我將最終結果粘貼下來:

[
{
    "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": [
                                {
                                }
                            ],
                        }
                    ]
                }
            ]
        }
    ]
}
]

為了消除這個最終結果的混亂,我想要的只是按他們的pluginId分組,然后是他們的pcVersion ,然后是他們的pluginVersion ,最后是他們的platform 我將粘貼最終結果的格式:

[
{
    "name": "pluginId", "children": [
        {
            "name": "pcVersion", "children": [
                {
                    "name": "pluginVersion", "children": [
                        {
                            "name": "mac", "children": [
                                {
                                }
                            ],
                            "name": "windows", "children": [
                                {
                                }
                            ],
                            "name": "linux", "children": [
                                {
                                }
                            ],
                        }
                    ]
                }
            ]
        }
    ]
}
]

我發現了很棒的 Lodash 庫 groupBy方法。 但是我必須為它寫很多糟糕的循環,而且它太復雜了。

我真的很感激任何解決它的提示或線索,因為我現在已經堅持了很多小時並且非常沮喪。 提前致謝。

這是一個相當緊湊的示例,使用嵌套的reduce()調用並將描述層次結構的鍵數組傳遞給內部reduce() 這允許您通過傳遞不同的層次結構數組以多種方式重構原始數組。

 const srcArr = [{ "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" }] function refactorArray(arr, tree) { return arr.reduce((a, o) => { const inner = tree.reduce((ka, k) => { const match = ka.find(e => e.name === o[k]); if (match) { ka = match.children; } else { const n = { name: o[k], children: [] }; ka.push(n); ka = n.children; } return ka; }, a); inner.push({ ...o }); return a; }, []); } const tree1 = [ 'pluginId', 'pcVersion', 'pluginVersion', 'platform']; console.log(refactorArray(srcArr, tree1)); const tree2 = [ 'platform', 'pluginVersion']; console.log(refactorArray(srcArr, tree2));

或者使用bind()創建專用函數...

 const srcArr = [{ "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" }] function refactorArrayByBoundTree(arr) { return arr.reduce((a, o) => { const inner = this.reduce((ka, k) => { const match = ka.find(e => e.name === o[k]); if (match) { ka = match.children; } else { const n = { name: o[k], children: [] }; ka.push(n); ka = n.children; } return ka; }, a); inner.push({ ...o }); return a; }, []); } const tree1 = [ 'pluginId', 'pcVersion', 'pluginVersion', 'platform']; const refactorByTree1 = refactorArrayByBoundTree.bind(tree1); console.log(refactorByTree1(srcArr)); const tree2 = [ 'platform', 'pluginVersion']; const refactorByTree2 = refactorArrayByBoundTree.bind(tree2); console.log(refactorByTree2(srcArr));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM