I've an multidimensional array in JavaScript, I'm filtering same array using reduce method & I've 2 parameters to filter the array.
2 filter parameters are - position, team. So I did it using below code.
var filterdProjectionsData = await ProjectionsData.reduce(function(filtered, Projection) {
if (Position && Projection.Position === Position && !Team) {
filtered.push(Projection);
}else if (Team && Projection.Team === Team && !Position) {
filtered.push(Projection);
}else if ((Position && Projection.Position === Position) && (Team && Projection.Team === Team)) {
filtered.push(Projection);
}
return filtered;
}, []);
I can manage easily with 1-2 filter parameters, but I'm getting issue with 3-4-5 filter parameters, in this case no of if/else condition combination will increase & not possible to maintain all if else statement in case of greater then 2-3 filter parameters.
So is there any alternative way to manage these conditions in very easy manner ? filter parameters are optional (not compulsory), so please let me know about the solution ?
await?filtered.push(Projection). So what you can do is , check the condition where you do not need to push it. Something like this:if(some condition) do nothing else filtered.push()