My Object:
const searchFor = ['appInfo', 'questions'];
My Data:
const data = [
{ id: '1', data: 'jobInfo' },
{ id: '2', data: 'appInfo' },
{ id: '3', data: 'documents' },
{ id: '4', data: 'questions' },
];
I am expecting the final result to be:
[
{ id: '2', data: 'appInfo' },
{ id: '4', data: 'questions' },
];
My try was that I can filter with one item with fixed value
const result = Object.keys(searchFor).map((key) => data.filter((obj) => obj.data === key))
I getting array of arrays
but I need array of object
and more over I not sure if there is any better way to do this.
Thanks in advance:)
You may use Array.prototype.filter()
together with Array.prototype.includes()
:
const searchFor = ['appInfo', 'questions'], data = [{id:'1',data:'jobInfo'},{id:'2',data:'appInfo'},{id:'3',data:'documents'},{id:'4',data:'questions'},], result = data.filter(({data:d}) => searchFor.includes(d)) console.log(result)
.as-console-wrapper{min-height:100%;}
You could take a Map
for the key/objects pairs and map the found objects.
const searchFor = ['appInfo', 'questions'], data = [{ id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }], result = searchFor.map( Map.prototype.get, new Map(data.map(o => [o.data, o])) ); console.log(result);
You can use the Array methods .filter
and .includes
(because searchFor
is an Array.)
const searchFor = ['appInfo', 'questions']; const data = [ { id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }, ]; const result = data.filter(item => searchFor.includes(item.data)); console.log(result);
You can do this:
const searchFor = ['appInfo', 'questions']; const data = [ { id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }, ]; var res = data.filter(i=> searchFor.includes(i.data)) console.log(res)
const searchFor = ['appInfo', 'questions']; const data = [ { id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents' }, { id: '4', data: 'questions' }, ]; var hashTable = {}; searchFor.forEach(i=> hashTable[i] = true ); var res = data.filter(i=>{ return hashTable[i.data] }); console.log(res)
You can use:
const results = data.filter(item => searchFor.includes(item.data)):
console.log(results):
const result = data.filter((rec, id) => {
if (searchFor.indexOf(rec.data) > -1) {
return rec
}
})
console.log(data.filter(element=>{
return searchFor.includes(element.data)
}));
The idea here is you have to take elements of data and then have to filter each element of data after matching with search for object.since search for the object that contains your filter criteria so it has to be nested within external iteration ie filter method we have passed on data object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.