I have an array of objects that I would like to filter and divide into groups according to the conditional. The problem arises because I have more than one conditional expression, and I would like the array to be divided into several arrays. The first array corresponds to the first conditional, the second array corresponds to the second conditional, ... and the last array containing all objects that do not match any conditional expression.
The first solution that came up with this problem was in the form of several .filter functions ...
var array = [{
name: 'X',
age: 18
}, {
name: 'Y',
age: 18
}, {
name: 'Z',
age: 20
}, {
name: 'M',
age: 20
}, {
name: 'W',
age: 5
}, {
name: 'W',
age: 10
}];
var matchedConditional1 = array.filter(function(x){
return x.age === 18;
});
var matchedConditional2 = array.filter(function(x){
return x.age === 20;
});
var matchedNoConditional = array.filter(function(x){
return (x.age !== 18 && x.age !== 20);
});
but it seemed redundant and not reused.
So, I changed the function on Brendan's answer and got this.
Array.prototype.group = function(f) {
var matchedFirst = [],
matchedSecond = [],
unmatched = [],
i = 0,
l = this.length;
for (; i < l; i++) {
if (f.call(this, this[i], i)[0]) {
matchedFirst.push(this[i]);
} else if (f.call(this, this[i], i)[1]) {
matchedSecond.push(this[i]);
} else {
unmatched.push(this[i]);
}
}
return [matchedFirst, matchedSecond, unmatched];
};
var filteredArray = array.group(function(x){
return [x.age === 18, x.age === 20];
});
3 . , , , , , .
, , , . , , , .
, , , - .
Ps. , , . . .map .reduce. .
: @slebetman, , .