通过内部数组元素重新分组

时间:2019-06-20 09:25:38

标签: javascript ecmascript-6 lodash

我有一个下面的json数据,我需要重新组合和排序。

我可以通过迭代数组并将它们推入另一个数组中来做到这一点,但是任何人都建议使用lodash或es6概念来做更简单的方法

实际数据是

 var foodItems= {
        "type": "Breakfast",
        "foods": [
            {
                "type": "Bread",
                "brand": "M"
            },
            {
                "type": "Milk",
                "brand": "Peter"
            },
            {
                "type": "Jam",
                "brand": "Super"
            }
        ]
    },
    {
        "type": "lunch",
        "foods": [
            {
                "type": "Noodles",
                "brand": "Fab Noodle"
            },
            {
                "type": "Yogurt",
                "brand": "Peter"
            },
            {
                "type": "Juice",
                "brand": "Super"
            }
        ]
    },
    {
        "type": "Dinner",
        "foods": [
            {
                "type": "Wrap",
                "brand": "M"
            },
            {
                "type": "Wine",
                "brand": "Super"
            },
             {
                "type": "IceCream",
                "brand": "Peter"
            }
        ]
    },
    {
        "foods": [
            {
                "type": "Snack",
                "brand": "M"
            },
            {
                "type": "water",
                "brand": "Super"
            }
        ]
    }

预期结果是

我需要按品牌分组,并在层次结构中按以下名称排序

例如:

- M

 - Bread
     * Breakfast
 - Wrap
     * Dinner
 - Snack

- Peter 
 - IceCream
     * Dinner
 - Milk
     * Breakfast
 - Yogurt
     * Lunch

0 个答案:

没有答案