首页 > 解决方案 > 具有多个索引打字稿的对象到数组

问题描述

我有一个 API 可以返回很多这样的对象

{ id : 1243, string: "things", bool: 1, date: "2021-21-11"}
{ id : 4233, string: "somethingselse", bool: 1, date: "2021-21-11"}

我可以有许多相同ID的数据

{ id : 1243, string: "other", bool: 0, date: "2021-27-10"}
{ id : 4233, string: "somethings else again", bool: 1, date: "2021-21-11"}

所以我做了一个函数来按 id 我的数据分组以获得类似的东西

object = {
        1243: [{
            0: {
                string: "things",
                bool: 1,
                date: "2021-21-11"
            },
            1: {
                string: "other",
                bool: 0,
                date: "2021-27-10"
            }
        }]

object2 = {
    4233: [{
        0: {
            string: "somethings else again",
            bool: 1,
            date: "2021-21-11"
        }
    }]



但现在我想要这样的东西

    alldata = [{
        1243: [{
            "2021-21-11": {
                string: "things",
                bool: 1
            },
            "2021-27-10": {
                string: "other",
                bool: 0
            }
        }],
        4233: [{
            "2021-21-11": {
                string: "somethings else again",
                bool: 1,
            }
        }]
    }]

我不知道该怎么做,我试图生成数组,对象,但没什么大不了的

标签: javascriptarraysangulartypescriptobject

解决方案


推荐阅读