0

I have an array of multiple objects as follows (only showing two for brevity):

const jobs = [
    {
      "id": 1,
      "company": "Photosnap",
      "new": true,
      "featured": true,
      "position": "Senior Frontend Developer",
      "role": "Frontend",
      "level": "Senior",
      "postedAt": "1d ago",
      "contract": "Full Time",
      "location": "USA Only",
      "languages": ["HTML", "CSS", "JavaScript"],
      "tools": ["React", "Sass"]
    },
    {
      "id": 2,
      "company": "Manage",
      "logo": "./images/manage.svg",
      "new": true,
      "featured": true,
      "position": "Fullstack Developer",
      "role": "Fullstack",
      "level": "Midweight",
      "postedAt": "1d ago",
      "contract": "Part Time",
      "location": "Remote",
      "languages": ["Python"],
      "tools": ["React"]
    },

And an array of keywords

const keywords = ['React', 'Fullstack', 'Vue']

I would like to filter my jobs array to return any job which contains ANY of the keywords in my keywords array.The keyword array is dynamic and may only include one term or several. I think the array.prototype.filter is my starting point but I cannot figure out how to return objects which include any of my keywords.

  let result = jobs.filter(function(obj) {
      ...
  })`
 
Could someone help?
4

1 Answer 1

2

You can use filter, some and includes

let jobs = [{"id": 1,"company": "Photosnap","new": true,"featured": true,"position": "Senior Frontend Developer","role": "Frontend","level": "Senior","postedAt": "1d ago","contract": "Full Time","location": "USA Only","languages": ["HTML", "CSS", "JavaScript"],"tools": ["React", "Sass"]},{"id": 2,"company": "Manage","logo": "./images/manage.svg","new": true,"featured": true,"position": "Fullstack Developer","role": "Fullstack","level": "Midweight","postedAt": "1d ago","contract": "Part Time","location": "Remote","languages": ["Python"],"tools": ["React"]}, {"id": 3,"company": "Manage","logo": "./images/manage.svg","new": true,"featured": true,"position": "Fullstack Developer","role": "Fullstack","level": "Midweight","postedAt": "1d ago","contract": "Part Time","location": "Remote","languages": ["Python"],"tools": ["Angular"]}]
const keywords = ['React', 'Fullstack', 'Vue']

let results = jobs.filter(job => {
  return job.tools.some(v => keywords.includes(v))
})

console.log(results)

You can improve time complexity by using Set

let jobs = [{"id": 1,"company": "Photosnap","new": true,"featured": true,"position": "Senior Frontend Developer","role": "Frontend","level": "Senior","postedAt": "1d ago","contract": "Full Time","location": "USA Only","languages": ["HTML", "CSS", "JavaScript"],"tools": ["React", "Sass"]},{"id": 2,"company": "Manage","logo": "./images/manage.svg","new": true,"featured": true,"position": "Fullstack Developer","role": "Fullstack","level": "Midweight","postedAt": "1d ago","contract": "Part Time","location": "Remote","languages": ["Python"],"tools": ["React"]}, {"id": 3,"company": "Manage","logo": "./images/manage.svg","new": true,"featured": true,"position": "Fullstack Developer","role": "Fullstack","level": "Midweight","postedAt": "1d ago","contract": "Part Time","location": "Remote","languages": ["Python"],"tools": ["Angular"]}]
const keywords = new Set(['React', 'Fullstack', 'Vue'])

let results = jobs.filter(job => {
  return job.tools.some(v => keywords.has(v))
})

console.log(results)

Sign up to request clarification or add additional context in comments.

5 Comments

will need to check includes of Object.values(job).flat() to capture Fullstack keyword
@cmgchess if we want to match with all the properties, then we need to consider what different type of value properties can hold and then accordingly, need to check each of them, i have added this as an example to show how to proceed further
This is a duplicate multiple times over. Flag to close
Thank you Code Maniac. The only problem with this solution is that it is just checking the tools key. What I'm trying to achieve is simply whether a job object contains ANY of the keywords, ANYWHERE in its values. You have certainly helped though, thank you
@danielCerulean see the flagged duplicate Filter array of objects by multiple strings for discussion around searching all properties

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.