0

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!

3 Answers 3

2
    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
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

You are only checking if the user is the same. But I need to check if multiple key/values are equal. Maybe you can edit your code to fit my needs.
Updated the answer @riXon
lodash is not really shorter than vanilla. and you need two loops.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.