0
 {nDATA
          .filter((newData) => newData.titles.includes(props.currentFilter))
          .map((value, index) => (
    

So in the code above I have nDATA which is an array of objects that I am filtering and mapping out.

Then I also have props.currentFilter which is a prop being passed into this component that is an array list for example

["Apples", "Oranges", "Strawberries", "Grapes"]

What I am trying to do here is get my filter setup here to check each newData.titles for each of the arrays in props.currentFilter. That way when I am searching for say Apples and Oranges and Grapes, it will check each thing for each of those and if it has 1 of them it goes through. My setup right now is working perfect if I pass just a string into the includes by doing props.currentFilter[0] but that does me no good for what I am trying to do here. I have been trying to test a few ways to do this and have been struggling to figure this out, any help would be much appreciated =D

5
  • Is newData.titles a string? Commented Jul 14, 2020 at 2:35
  • Yeah its a string Commented Jul 14, 2020 at 2:50
  • If it is a string then iamuchejude's answer should work... if it's an array then you can try: .filter(newData => props.currentFilter.some(str => newData.titles.includes(str))) Commented Jul 14, 2020 at 2:52
  • Ah maybe its an array then ill try that Commented Jul 14, 2020 at 2:53
  • 1
    Looks like it was an array lol. That worked perfect, post that as an answer and ill give ya checkmark. I appreciate your time greatly! Commented Jul 14, 2020 at 2:54

3 Answers 3

2

You can use .some() in your filter method to check if some value from your props.currentFilter is included within your newData.titles array:

nDATA
  .filter(newData => props.currentFilter.some(str => newData.titles.includes(str)))
Sign up to request clarification or add additional context in comments.

Comments

0
nData.filter(newData => props.currentFilter.includes(newData.titles))

1 Comment

Does not seem to work but I am trying to see if maybe I can see why its not working lol. Also I fixed the nDATA so its not that. Its just coming back as no results
0

An optimal way to run this function would be to turn your currentFilter array into an object with the keys being your filter words. Values can be anything.

let filterObj = props.currentFilter.reduce((acc, cur) => {
 acc[cur] = 1
 return acc
}, {})


which should return

filterObj = {
 "apples": 1,
 "oranges": 1,
 "bananas": 1,
}

Then run your filter loop and call hasOwnProperty method which returns a boolean

{nDATA
          .filter((newData) => currentFilter.hasOwnProperty(newData.titles))
          .map((value, index) => (

3 Comments

If you're using an object for the sole purpose of efficient look-up then you'd be better of using a Set for that :)
@NickParsons thanks for the tip! Do Sets and Objects in JS have different lookup runtimes?
They both should be the same, so you can expect O(1) for both .has() (on the set) and object lookup.

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.