Lodash create collection from duplicate object keys

拟墨画扇 提交于 2019-11-27 15:48:44

Use _.groupBy and then _.map the resulting object to an array of objects.

var newOutput = _(output)
    .groupBy('article')
    .map(function(v, k){ return { article: k, titles: _.map(v, 'title') } })
    .value();

var output = [{"article":"BlahBlah","title":"Another blah"},{"article":"BlahBlah","title":"Return of the blah"},{"article":"BlahBlah2","title":"The blah strikes back"},{"article":"BlahBlah2","title":"The blahfather"}];

let newOutput = _(output)
    .groupBy('article')
    .map(function(v, k){ return { article: k, titles: _.map(v, 'title') } })
    .value();

console.log(newOutput);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>

With ES6 arrow-functions,

var newOutput = _(output)
    .groupBy('article')
    .map((v, k) => ({ article: k, titles: _.map(v, 'title') }))
    .value();

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();

A proposal in plain Javascript

It uses a IIFE (Immediate Invoked Function Expression) for using private variables and for collecting the return values in an array.

Beside that it uses a hash table for the reference to the right array item.

var output = [{ article: "BlahBlah", title: "Another blah" }, { article: "BlahBlah", title: "Return of the blah" }, { article: "BlahBlah2", title: "The blah strikes back" }, { article: "BlahBlah2", title: "The blahfather" }],
    newOutput = function (data) {
        var r = [];
        data.forEach(function (a) {
            if (!this[a.article]) {
                this[a.article] = { article: a.article, titles: [] };
                r.push(this[a.article]);
            }
            this[a.article].titles.push(a.title);
        }, Object.create(null));
        return r;
    }(output);
        
console.log(newOutput);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!