I’m looping inside a filter. I want to get the values from my vals
array plus the keys(name, description) for my filter.
When I iterate through my vals
array, I keep getting returned the name
but not the key.
Ideally I would like the return method to give me key and value.
return x[this.searchValues[i]].includes('phil')
to be
return x.name.includes('phil')
return x.decription.includes('phil')
const vals = ['name', 'decription'] const arr =[{ name: 'joe', decription: 'is a guy who likes beer' }, name: 'phil', decription: 'is a super hero' }] this.result = arr.filter((x) => { for(let i = 0; i< vals.length; i++){ return x[this.searchValues[i]].includes('phil'); } })
Advertisement
Answer
const vals = ['name', 'decription'] const arr =[{ name: 'joe', decription: 'is a guy who likes beer' },{ name: 'phil', decription: 'is a super hero' }] let result = arr.filter(e => vals.some(n => e[n].includes('phil'))) console.log(result)