Lodash group by multiple properties if property value is true

前端 未结 8 1911
执笔经年
执笔经年 2020-12-10 02:20

I have an array of vehicles that need to be grouped by make and model, only if the \'selected\' property is true. The resulting object should contain properties for make mod

8条回答
  •  感情败类
    2020-12-10 02:54

    I am not sure if this will solve Your problem, but in group_by You can add custom logic which allow You to create composite key.

    Remember that SEPERATOR value should be defined accordingly to data source You work with, if "--" characters occur in model or type You should not used them as it will not allow You to reverse the process of grouping.

    const SEPERATOR = "--";
    _.chain(data).filter((item) => item.selected).groupBy((item)=>`${item.model}${SEPERATOR}${item.type}`).value();
    

提交回复
热议问题