0

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 ?

4
  • I don't think Array.reduce() is asynchronous, so why the await? Commented Aug 10, 2020 at 4:09
  • Ok, I'll remove this. Can you pls answer on this ? Commented Aug 10, 2020 at 4:12
  • For all conditions, you are doing exactly same thing i.e 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() Commented Aug 10, 2020 at 4:16
  • Please state all your conditions first. Commented Aug 10, 2020 at 4:20

1 Answer 1

2

This is a very simple little framework but you should be able to see how to extend it to the level of complexity required: Here you construct a 'filter' array and then use that to process your target array.

Right now this only applies equivalency and works with (and) right now but it's not hard to alter this to deal with basic boolean logic (or). The key is to construct your 'filter' array as KVPs - i.e. look for an attribute which compares to a value via an operator.

This way you can have as many or as few filter parameters as you want without writing a million if-else-if statements!

Edit: Added the operator value for additional flexibility.

let myArray = [
    {ID : 1, position : "tinker", salary : 40000},
    {ID : 2, position : "tinker", salary : 45000},
    {ID : 3, position : "tailor", salary : 30000},
    {ID : 4, position : "soldier", salary : 50000},
    {ID : 5, position : "spy", salary : 140000},
]

let filters = [
    {attribute: "position", value: "tinker", operator : "=="},
    {attribute: "salary", value: 41000, operator: ">"}  
]

function multiFilter(filters, array){
    filters.forEach(myFilter => {
      array = array.filter(arrayItem => eval("arrayItem." + myFilter.attribute + myFilter.operator + "'" + myFilter.value + "'"));
    });
    return array;
}

console.log(multiFilter(filters, myArray));
Sign up to request clarification or add additional context in comments.

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.