如何通过打字稿中的不同键对对象数组进行排序

时间:2019-07-01 17:12:10

标签: arrays json angular typescript angular-arrays

我正在用json创建对象数组,但没有(正确的)顺序。顺序取决于不同的键,也取决于应为其创建顺序的“级别” /键。有什么方法可以创建这样的订单吗?

数组是多个Angular Material Tables的数据源,其中对象应显示在不同的表中(因此我还必须创建一个数组数组)

数据本身看起来像:

power: [5,10,15]
pwoer2: [0,10,15]
power3: [1000,2000,3000]

我在json中的数组看起来像这样,甚至还有更多键:

"test_comp": [
            {
                "power": 5,
                "power2": 0,
                "power3": 1000
            },
            {
                "power": 10,
                "power2": 0,
                "power3": 1000
            },
            {
                "power": 15,
                "power2": 0,
                "power3": 1000
            },
            {
                "power": 5,
                "power2": 10,
                "power3": 1000
            },
            {
                "power": 10,
                "power2": 10,
                "power3": 1000
            },
            {
                "power": 15,
                "power2": 10,
                "power3": 1000
            },
            ...
            {
                "power": 15,
                "power2": 15,
                "power3": 3000
            }
]

但是我想要的是:

            {
                "power": 5,
                "power2": 0,
                "power3": 1000
            },
            {
                "power": 5,
                "power2": 0,
                "power3": 2000
            },
            {
                "power": 5,
                "power2": 0,
                "power3": 3000
            },
            {
                "power": 5,
                "power2": 10,
                "power3": 1000
            },
            {
                "power": 5,
                "power2": 10,
                "power3": 2000
            },
            {
                "power": 5,
                "power2": 10,
                "power3": 3000
            },
            {
                "power": 5,
                "power2": 15,
                "power3": 1000
            },
            ...
            {
                "power": 15,
                "power2": 15,
                "power3": 3000
            }
]

最后,我希望此数组将其保存为对象数组,但这在对主数组进行排序之后很容易

0 个答案:

没有答案