Lodash create collection from duplicate object keys

前端 未结 3 1031
轻奢々
轻奢々 2020-12-04 00:35

I have the following structure:

var output = [{
    \"article\": \"BlahBlah\",
    \"title\": \"Another blah\"
}, {
    \"article\": \"BlahBlah\",
    \"titl         


        
3条回答
  •  一生所求
    2020-12-04 01:03

    A better lodash version could be (using the awesome chaining approach)

    _(a).groupBy('article').map( (x,k) => ({ article: k, titles:_.map(x, 'title')}) ).value();  
    

    If you want to group by article (so article would be the key, useful for quick lookup)

    _(a).groupBy('article').mapValues(x => _.map(x, 'title')).value();
    

提交回复
热议问题