During work, I was given this task: to group elements with similar properties in the array.
In general, the problem is as follows:
var list = [
{name: "1", lastname: "foo1", age: "16"},
{name: "2", lastname: "foo", age: "13"},
{name: "3", lastname: "foo1", age: "11"},
{name: "4", lastname: "foo", age: "11"},
{name: "5", lastname: "foo1", age: "16"},
{name: "6", lastname: "foo", age: "16"},
{name: "7", lastname: "foo1", age: "13"},
{name: "8", lastname: "foo1", age: "16"},
{name: "9", lastname: "foo", age: "13"},
{name: "0", lastname: "foo", age: "16"}
];
If I group this elements by lastname
and age
, I will get this result:
var result = [
[
{name: "1", lastname: "foo1", age: "16"},
{name: "5", lastname: "foo1", age: "16"},
{name: "8", lastname: "foo1", age: "16"}
],
[
{name: "2", lastname: "foo", age: "13"},
{name: "9", lastname: "foo", age: "13"}
],
[
{name: "3", lastname: "foo1", age: "11"}
],
[
{name: "4", lastname: "foo", age: "11"}
],
[
{name: "6", lastname: "foo", age: "16"},
{name: "0", lastname: "foo", age: "16"}
],
[
{name: "7", lastname: "foo1", age: "13"}
]
];
After some experimentation, I came to the following solution:
Array.prototype.groupByProperties = function(properties){
var arr = this;
var groups = [];
for(var i = 0, len = arr.length; i<len; i+=1){
var obj = arr[i];
if(groups.length == 0){
groups.push([obj]);
}
else{
var equalGroup = false;
for(var a = 0, glen = groups.length; a<glen;a+=1){
var group = groups[a];
var equal = true;
var firstElement = group[0];
properties.forEach(function(property){
if(firstElement[property] !== obj[property]){
equal = false;
}
});
if(equal){
equalGroup = group;
}
}
if(equalGroup){
equalGroup.push(obj);
}
else {
groups.push([obj]);
}
}
}
return groups;
};
This solution works, but is this a right and best way? It still looks a little ugly to me.