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?
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)