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?
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Lim Han Yang Jun 25 '22 at 12:18
  • Hi, I don't think it does - that solution is just sorting based on specific key:value, I can not know if a keyword will be in "role" key or "contract" etc etc, hence why i thought a simple includes might be the way. – danielCerulean Jun 25 '22 at 12:23
  • also: [Filter array of objects by multiple strings](https://stackoverflow.com/questions/49041678/filter-array-of-objects-by-multiple-strings) which searches all props for the strings or [Filter array of objects whose any properties contains a value](https://stackoverflow.com/questions/44312924/filter-array-of-objects-whose-any-properties-contains-a-value) or – pilchard Jun 25 '22 at 12:36
  • Hi pilchard, i think https://stackoverflow.com/questions/49041678/filter-array-of-objects-by-multiple-strings might have done the trick – danielCerulean Jun 25 '22 at 13:04

1 Answers1

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)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • will need to check `includes` of `Object.values(job).flat()` to capture Fullstack keyword – cmgchess Jun 25 '22 at 12:26
  • 1
    @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 – Code Maniac Jun 25 '22 at 12:30
  • This is a duplicate multiple times over. Flag to close – pilchard Jun 25 '22 at 12:39
  • 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 Jun 25 '22 at 12:46
  • @danielCerulean see the flagged duplicate [Filter array of objects by multiple strings](https://stackoverflow.com/questions/49041678/filter-array-of-objects-by-multiple-strings) for discussion around searching all properties – pilchard Jun 25 '22 at 12:50