rip999
rip999

Reputation: 5

How to group objects into one object in underscore

How to convert this :

[
  {name: 'foo', type: 'product'},
  {name: 'bar', type: 'product'},
  {name: 'john', type: 'product'},
  {name: 'no name', type: 'product'},
]

How can i group and get all the objects without name:'no name' as separate Object like this:

{  
  0:[
    {name: 'foo', type: 'product'},
    {name: 'bar', type: 'product'},
    {name: 'john', type: 'product'}
  ],

  1:[
    {name: 'no name', type: 'product'},
  ]
}

Upvotes: 0

Views: 252

Answers (5)

joews
joews

Reputation: 30330

To produce an object with keys 0 and 1, use _.groupBy:

var objectResult = _.groupBy(data, function(d) { return +(d.name === "no name")  })

To produce an array with two elements (which will also have keys 0 and 1) you could use _.partition (Underscore 1.6.0 +):

partition_.partition(array, predicate): Split array into two arrays: one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.

var arrayResult = _.partition(data, function(d) { return d.name !== "no name"  })

JSBin

Upvotes: 2

cpugourou
cpugourou

Reputation: 781

Why use a framework for such a simple thing ? Here is a pure JS solution allowing to groupBy properly. It is then easy to work with the result object.

var array=[
  {name: 'foo', type: 'product'},
  {name: 'bar', type: 'product'},
  {name: 'john', type: 'product'},
  {name: 'no name', type: 'product'},
  {name: 'foo', type: 'product'},
  {name: 'bar', type: 'product'},
  {name: 'john', type: 'product'},
  {name: 'no name', type: 'product'},
  {name: 'no name', type: 'product'},
  {name: 'no name', type: 'product'},
  {name: 'no name', type: 'product'},
];        

function groupBy(propertyName, array) {
var groupedElements = {};
for (var i = 0, len = array.length; i < len; i++) {
    var el = array[i];
    var value = el[propertyName].trim();
    var group = groupedElements[value];
    if (group === undefined) {
        group = [el];
        groupedElements[value] = group;
    } else {
        group.push(el);
    }
}
return groupedElements;
}

var result = groupBy('name',array);

Upvotes: 0

RobG
RobG

Reputation: 147363

As a comparison, plain ECMAScript takes just a little more code:

data.reduce(function(acc, obj) {
  acc[obj.name == 'no name'? 1:0].push(obj);
  return acc;},{0:[],1:[]}
);

Upvotes: 1

Isabek Tashiev
Isabek Tashiev

Reputation: 1044

You can try like that.

var arr = [
 {name: 'foo', type: 'product'},
 {name: 'bar', type: 'product'},
 {name: 'john', type: 'product'},
 {name: 'no name', type: 'product'},
];

var result = _.groupBy(arr, function (elem) { 
 return elem.name == 'no name' ? 1: 0;
});

Upvotes: 0

Dmitry Sokurenko
Dmitry Sokurenko

Reputation: 6122

You can try http://underscorejs.org/#groupBy:

var data = [
  {name: 'foo', type: 'product'},
  {name: 'bar', type: 'product'},
  {name: 'john', type: 'product'},
  {name: 'no name', type: 'product'},
]

var results = _.groupBy(data, function(obj) { return obj.name == 'no name' ? 1 : 0; })

console.log(results)

Upvotes: 0

Related Questions