1

I have a Profiles array of objects that I would like to filter. The filter is a combination of Statuses in another array. The array of Statuses is defined by the user. The Profiles array can be filtered with one or more Statuses by the user. How can I write a filter function without knowing how many conditions should be checked in advance?

//the array to filter
let profileList = [{name: "John", staus: "green"}, {name: "Alex", staus: "yellow"}, {name: "Jane", staus: "green"}]

//user defined: they can add up to 30 statuses
let statusList = ["green", "yellow", "red", "black"]

//also user defined: they can filter by as many statuses as they want
let filters = ["green", "red"]

  
const filteredProfileList = profileList.filter(element => {
  return //how to write the conditions with logical OR (||) if I don't know how many statuses will be used?
});
    


1 Answer 1

1

You don't need to know how many will be checked, you can just check if the array of filters includes the profiles status:

//the array to filter
let profileList = [{name: "John", staus: "green"}, {name: "Alex", staus: "yellow"}, {name: "Jane", staus: "green"}]

//user defined: they can add up to 30 statuses
let statusList = ["green", "yellow", "red", "black"]

//also user defined: they can filter by as many statuses as they want
let filters = ["green", "red"]

  
const filteredProfileList = profileList.filter(element => {
  return filters.includes(element.staus);
});

console.log(filteredProfileList)

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

1 Comment

Thanks a lot, this works perfectly is exaclty what I needed !

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.