Skip to main content
typo
Source Link
Mr Shumar
  • 261
  • 1
  • 4

May I suggest one more approach, using Array.reduce, as chaining Array.filters will go over whole array every time that way which can be resource expensive if arrays are huge.

Example:

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

const filteredArray = arrayToFilter.reduce((accumulator, currentItem) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](currentItem)) {
      return accumulator
    }
  }
  return [...accumulator, currentItem]
}, [])

EDIT: After sleeping a while, I noticed that you can actually use Array.filter in similar way as reduce, without going multiple times over the array.

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

const filteredArray = arrayToFilter.filter((item) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](item)) {
      return false
    }
  }
  return true
})

May I suggest one more approach, using Array.reduce, as chaining Array.filters will go over whole array every time that way which can be resource expensive if arrays are huge.

Example:

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

const filteredArray = arrayToFilter.reduce((accumulator, currentItem) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](currentItem)) {
      return accumulator
    }
  }
  return [...accumulator, currentItem]
}, [])

May I suggest one more approach, using Array.reduce, as chaining Array.filters will go over whole array every time that way which can be resource expensive if arrays are huge.

Example:

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

const filteredArray = arrayToFilter.reduce((accumulator, currentItem) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](currentItem)) {
      return accumulator
    }
  }
  return [...accumulator, currentItem]
}, [])

EDIT: After sleeping a while, I noticed that you can actually use Array.filter in similar way as reduce, without going multiple times over the array.

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

const filteredArray = arrayToFilter.filter((item) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](item)) {
      return false
    }
  }
  return true
})
Minor code changes for better understanding
Source Link
Mr Shumar
  • 261
  • 1
  • 4

May I suggest one more approach, using Array.reduce, as chaining Array.filters will go over whole array every time that way which can be resource expensive if arrays are huge.

Example:

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

const filteredArray = arrayToFilter.reduce((accumulator, currentItem) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](currentItem)) {
      return accumulator
    }
  }
    return [...accumulator, currentItem]
}, [])

May I suggest one more approach, using Array.reduce, as chaining Array.filters will go over whole array every time that way which can be resource expensive if arrays are huge.

Example:

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

arrayToFilter.reduce((accumulator, currentItem) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](currentItem)) {
      return accumulator
    }
  }
    return [...accumulator, currentItem]
}, [])

May I suggest one more approach, using Array.reduce, as chaining Array.filters will go over whole array every time that way which can be resource expensive if arrays are huge.

Example:

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

const filteredArray = arrayToFilter.reduce((accumulator, currentItem) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](currentItem)) {
      return accumulator
    }
  }
  return [...accumulator, currentItem]
}, [])
Source Link
Mr Shumar
  • 261
  • 1
  • 4

May I suggest one more approach, using Array.reduce, as chaining Array.filters will go over whole array every time that way which can be resource expensive if arrays are huge.

Example:

const filterMethods = [
  (item => item.category_id.includes(selectedCategoryOptions)),
  (item => item.discounts.length > 0),
  ((item) => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) != -1)
]

arrayToFilter.reduce((accumulator, currentItem) => {
  for (let i = 0; i < filterMethods.length; i++) {
    if (!filterMethods[i](currentItem)) {
      return accumulator
    }
  }
    return [...accumulator, currentItem]
}, [])