Can't seem to return what I wanting. Eventually, I want to return an array of objects, or a single object that matches possibly two different partial key/value filters. In other words: const curQuery = {name:'Nex', tacTechnology: 'Dat'}
I'm planning on having multiple filters in the header of a table.
const data = [{
"id": "1",
"name": "Unified Communications Portal",
"tacTechnology": "Collaboration"
}, {
"id": "2",
"name": "ACE Training Portal",
"tacTechnology": "Networking Services"
}, {
"id": "3",
"name": "Nexus 5K/2K Training Portal",
"tacTechnology": "Data Center"
}, {
"id": "4",
"name": "XR Training Portal",
"tacTechnology": "Routing"
}]
const curQuery = {name:'Nex'}
function setFilteredItems(curQuery) {
const curQueryKeys = Object.keys(curQuery)
const filteredItems = data.filter(
(item) => {
curQueryKeys.forEach((objKey) => {
if (item[objKey] === undefined || curQuery[objKey] === null){
console.log('its not', item[objKey], curQuery[objKey])
return false;
}
else if(item[objKey].toLowerCase().includes(curQuery[objKey].toLowerCase())) {
console.log('it includes', item[objKey], curQuery[objKey])
return item;
}
})
}
)
console.log('filteredItems', filteredItems )
}
setFilteredItems(curQuery);
Expecting filteredItems to be { "id": "3", "name": "Nexus 5K/2K Training Portal", "tacTechnology": "Data Center" }, but getting nothing back.
I created a quick codepen here: https://codepen.io/bmarker/pen/mddEdma