coderocket
coderocket

Reputation: 584

Build new array of objects with array containing unique data from array of objects with duplicate values of specific keys

I need your help... I got an Array of Objects looking something like this:

var arr = [{    
  title: 'My title',
  user: 1,
  price: 22,
  location: 'Berlin'
},{    
  title: 'My title',
  user: 1,
  price: 18,
  location: 'Cologne'
},{    
  title: 'My title',
  user: 1,
  price: 26,
  location: 'Hamburg'
},{    
  title: 'Other Title',
  user: 2,
  price: 26,
  location: 'Frankfurt'
},{    
  title: 'Other Title',
  user: 2,
  price: 28,
  location: 'Munich'
},];

Now I want to build a new Array of Objects that will look like this:

var result = [{    
  title: 'My title',
  user: 1,
  events: [
    {
      price: 22,
      location: 'Berlin'
    }, {
      price: 18,
      location: 'Cologne'
    }, {
      price: 26,
      location: 'Hamburg' 
    }
  ]

},{    
  title: 'Other Title',
  user: 2,
  events: [
    {
      price: 28,
      location: 'Munich'
    },{
      price: 26,
      location: 'Frankfurt'
    }
  ]
}];

I need to group the objects by multiple values, like in my example by user and title and add the unique data of them to a new field.

If someone could show me how to do that with lodash would be awesome!

Thank you for your help!

Upvotes: 0

Views: 69

Answers (3)

Andy
Andy

Reputation: 63587

Lodash answer:

function remap(arr) {
  var out = _.reduce(arr, function(p, c) {
    var key = [c.user, c.title].join('|');
    p[key] = p[key] || { title: c.title, user: c.user, events: [] };
    p[key].events.push({ price: c.price, location: c.location });
    return p;
  }, {});
  return _.map(_.keys(out), function(el) {
    return out[el];
  });
}

remap(arr);

DEMO

Upvotes: 1

Dmitriy
Dmitriy

Reputation: 3765

    arr.reduce(function (hash, item) {
        var key = item.title + item.user;
        var obj = hash[key] || {};
        obj.title = item.title;
        obj.user = item.user;
        obj.events = obj.events || [];
        obj.events.push({
            price: item.price,
            location: item.location
        });
        hash[key] = obj;
        return hash;
    }, {});

    var result = [];
    for (var key in arr) {
        result.push(arr[key]);
    }

    console.log(result); // the result array

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386868

This is a proposal in plain Javascript with a temporary object for the references to the result array.

var arr = [{ title: 'My title', user: 1, price: 22, location: 'Berlin' }, { title: 'My title', user: 1, price: 18, location: 'Cologne' }, { title: 'My title', user: 1, price: 26, location: 'Hamburg' }, { title: 'Other Title', user: 2, price: 26, location: 'Frankfurt' }, { title: 'Other Title', user: 2, price: 28, location: 'Munich' }],
    grouped = function (array) {
        var r = [], o = {};
        array.forEach(function (a) {
            if (!o[a.user]) {
                o[a.user] = { title: a.title, user: a.user, events: [] };
                r.push(o[a.user]);
            }
            o[a.user].events.push({ price: a.price, location: a.location });
        });
        return r;
    }(arr);

document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

Upvotes: 1

Related Questions