1

I have a questions base on previous questions here.

There I'm using Python to do that, but now I want to convert the code to Javascript code.

So basically my questions there are, let's say I have an array like this:

job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']

And now I want to search with multiple keywords in my array, e.g:

When I try to input the keyword teacher and sales, it should return result like this:

  • schoolteacher
  • mathematics teacher
  • salesperson
  • sales manager

So, how to do that in Javascript?

Jack Bashford
44.3k11 gold badges56 silver badges84 bronze badges
asked Apr 15, 2019 at 2:44

3 Answers 3

3

Use filter and some:

const job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher'];
const getJobs = (...words) => job_list.filter(s => words.some(w => s.includes(w)))
console.log(getJobs("teacher", "sales"));

answered Apr 15, 2019 at 2:47
Sign up to request clarification or add additional context in comments.

Comments

1

You can make a regular expression that describes what you want and filter accordingly with test():

let job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']
let filtered = job_list.filter(job => /teacher|sales/.test(job))
console.log(filtered)

answered Apr 15, 2019 at 2:51

Comments

0

You can use filter like this:

job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']
console.log(job_list.filter(word => word.includes("teacher") || word.includes("sales")))

Complexity: O(N)

answered Apr 15, 2019 at 2:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.