1

I have a search function that takes input/query text from a search bar. I want it to work for multiple search terms like "javascript react" or with more or less search terms. The input is saved in an array in state ("query") and compares to an object "workitem" and its property "description".

Let say:

   workitem.description.includes(this.state.query)

where

  this.state.query // = ["react", "javacript"]

Above will only work for certain situations. I want to see if the array/object includes ANY elements of the state. How to do it?

1
  • you can use indexof Commented Oct 10, 2019 at 11:03

2 Answers 2

2
// if needed, do a 
// if (!workitem.description || !this.state.query) {
//     return false;
// }

Considering description is an array:

return workitem.description.some(desc => this.state.query.indexOf(desc) >= 0)

Considering description is a string:

return workitem.description
    .split(' ')
    .some(desc => state.query.indexOf(desc) >= 0);
Sign up to request clarification or add additional context in comments.

2 Comments

It seems description is a string not an array.
let me do a version for both
1

How about this:

workitem.description.split(' ').some(str => this.state.query.includes(str)) 

Comments

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.