�
�
­2019-04-26 00:41:55
JavaScript
­, 2019-04-26 00:41:55

How to filter by one or more parameters?

I have an example array of objects:

data = [

  { bg: 'black', color: 'green' },
  { bg: 'black', color: 'green' },
  { bg: 'black', color: 'red' },
  { bg: 'black', color: 'red' },

  { bg: 'white', color: 'green' },
  { bg: 'white', color: 'green' },
  { bg: 'white', color: 'red' },
  { bg: 'white', color: 'red' }
  
]

And there are 2 filters:
5cc227bf6b950185290186.jpeg
The goal is this.
When the first filter is selected, for example background black , it will only output:
[
    { bg: 'black', color: 'green' },
    { bg: 'black', color: 'green' },
    { bg: 'black', color: 'red' },
    { bg: 'black', color: 'red' }
]

And when another filter is added to this, for example color red , it will, accordingly, produce:
[
    { bg: 'black', color: 'red' },
    { bg: 'black', color: 'red' }
]

Actually, how to implement it?
I set filters in something like this:
filters = {
  bg: 'white', // or 'none', or 'black'
  color: 'none', // or 'red, or 'green'
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-04-26
@640

https://jsfiddle.net/3pb51azj/

S
Sergey Ganin, 2019-04-26
@dohera

const filterColor = chosedColor => currColor => chosedColor === 'none' ? true : currColor === chosedColor
arr.filter(filterColor(inputValue1)).filter(filterColor(inputValue2))

more or less like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question