Javascript 对象保留关键帧上的Lodash groupBy

Javascript 对象保留关键帧上的Lodash groupBy,javascript,lodash,Javascript,Lodash,在对象关键帧上使用Lodash u2;.groupBy方法时,我希望保留关键帧 假设我有一个对象: foods = { apple: { type: 'fruit', value: 0 }, banana: { type: 'fruit', value: 1 }, broccoli: { type: 'vegetable', value: 2 } }

在对象关键帧上使用Lodash u2;.groupBy方法时,我希望保留关键帧

假设我有一个对象:

foods = {
    apple: {
        type: 'fruit',
        value: 0
    },
    banana: {
        type: 'fruit',
        value: 1
    },
    broccoli: {
        type: 'vegetable',
        value: 2
    }
}
我想做一个转换来获得输出

transformedFood = {
    fruit: {
        apple: {
            type: 'fruit',
            value: 0
        },
        banana: {
            type: 'fruit',
            value: 1
        }
    },
    vegetable: {
        broccoli: {
            type: 'vegetable',
            value: 2
        }
    }
}
执行
transformedFood=uu.groupBy(foods,'type')
将提供以下输出:

transformedFood = {
    fruit: {
        {
            type: 'fruit',
            value: 0
        },
        {
            type: 'fruit',
            value: 1
        }
    },
    vegetable: {
        {
            type: 'vegetable',
            value: 2
        }
    }
}
请注意原始关键点是如何丢失的。有谁知道一种优雅的方法可以做到这一点,最好是在单行lodash函数中

var transformedFood = _.transform(foods, function(result, item, name){  
        result[item.type] = result[item.type] || {}; 
        result[item.type][name] = item;
});

如果使用lodash fp,请不要忘记最后一个参数
累加器