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')
JavaScript
x
16
16
1
const vals = ['name', 'decription']
2
3
const arr =[{
4
name: 'joe',
5
decription: 'is a guy who likes beer'
6
},
7
name: 'phil',
8
decription: 'is a super hero'
9
}]
10
11
this.result = arr.filter((x) => {
12
for(let i = 0; i< vals.length; i++){
13
return x[this.searchValues[i]].includes('phil');
14
}
15
})
16
Advertisement
Answer
JavaScript
1
13
13
1
const vals = ['name', 'decription']
2
3
const arr =[{
4
name: 'joe',
5
decription: 'is a guy who likes beer'
6
},{
7
name: 'phil',
8
decription: 'is a super hero'
9
}]
10
11
let result = arr.filter(e => vals.some(n => e[n].includes('phil')))
12
13
console.log(result)