1

Using a multi select dropdown where i have an array of objects where i want to filter it out based on user input such as

[{
        "ServiceArea": "NON-CIO",
        "ManagingDirector": "qwe",
        "Lead": "abc",
        "Manager": "xyz"
        "id":"1",
        "Designation":"COO"
    },
    {
        "ServiceArea": "NON-CIO",
        "ManagingDirector": "dfg",
        "Lead": "hgf",
        "Manager": "lkj"
        "id":"2",
        "Designation":"CTO"
    },
    {
        "ServiceArea": "NON-CIO",
        "ManagingDirector": "out",
        "Lead": "poi",
        "Manager": "uyt",
        "id":"43",
        "Designation":"COO"
    },
    {
        "ServiceArea": "4500-CIO",
        "ManagingDirector": "yhh",
        "Lead": "trr",
        "Manager": "nbb"
        "id":"403",
        "Designation":"CTO"
    }
]

Custom user input 1st time-

ServiceArea = ["NON-CIO"]

I should be getting first three records. Second time user inputs

ManagingDirector = ["dgf","qwe"]

Here i should be getting first two records. I am using this function but it seems to append the array not replace.

//airData is main array

array.forEach((item)=>{
 var name = this.airData.filter( el=>el.ManagingDirector == item );
 this.airData.push.apply(this.finalArr,name);
});

6
  • 1
    Please post ONE minimal reproducible example of your attempt, noting input and expected output using the [<>] snippet editor you now use just for formatting Commented Dec 16, 2022 at 7:57
  • I have mentioned 2 inputs as its a cascading multiselect so i need to filter the array as user selects the options. Commented Dec 16, 2022 at 8:00
  • 1
    Also why push in a filter? Just use the result of the filtering. And use something like result = listYouAreFiltering.filter(({ManagingDirector}) => userInputArray.includes(ManagingDirector)) Commented Dec 16, 2022 at 8:00
  • If the user input is ["a","s"] i used the loop to find the object and push into the final array. Can you suggest any other method? Commented Dec 16, 2022 at 8:10
  • When array = ["dgf","qwe"], you mentioned you should be getting first two records. That looks like a typo. qwe is in the first and third objects. And the second object has dfg not dgf. So, you'll get the first and third objects in that case. Commented Dec 16, 2022 at 8:10

1 Answer 1

2

Assuming a filters object like this

const data = [{ServiceArea:"NON-CIO",ManagingDirector:"qwe",Lead:"abc",Manager:"xyz"},{ServiceArea:"NON-CIO",ManagingDirector:"dfg",Lead:"hgf",Manager:"lkj"},{ServiceArea:"NON-CIO",ManagingDirector:"qwe",Lead:"poi",Manager:"uyt"},{ServiceArea:"4500-CIO",ManagingDirector:"yhh",Lead:"trr",Manager:"nbb"}];

const filters = {
  ServiceArea: ["NON-CIO"],
  ManagingDirector: ["dgf", "qwe"],
  Manager: []
}

const result = data.filter(e =>
  Object.entries(filters).every(([key, vals]) =>
    vals.length === 0 || vals.includes(e[key])))

console.log(result)

Sign up to request clarification or add additional context in comments.

8 Comments

Hi, will this handle empty array scenario too. If user decides to lets say remove managingdirector input from UI. The array becomes blank now only servicearea should work.
@RahulJain: no, you need something like val.length === 0 || vals.includes...
Can you give an example? As i have 6 different arrays where input gets stored and user can anytime deselect or select which will make each array sometimes empty also.
Aslo if i don't have any value in the beginning .includes() gives error of does not exist on type unknown
@RahulJain that's a ts error, you can search for it. There are plenty of examples
|

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.