Currently, I am in the process of developing a customized search feature that involves working with an array of objects.
const data = [{
name: "Janet McKenly",
age: 49,
city: "Baltimore",
active: "2019-02-15",
role: "Admin. Assistant"
},
{
name: "Eric Brown",
age: 23,
city: "Dallas",
active: "2020-06-01",
role: "Janitor"
},
{
name: "Nora Payne",
age: 41,
city: "Los Angeles",
active: "2020-10-02",
role: "Sales Associate"
}
]
In addition to this, there is another array which is being generated dynamically and serves as a restriction on the specific parameters allowed for searching purposes.
let searchColumnOnlyArray = ["name", "city", "active"]; // (generated dynamically)
It should be noted that role
& age
are excluded from being searchable.
The primary objective here is to conduct searches exclusively within the values associated with keys mentioned in the searchColumnOnlyArray
.
Although I have successfully implemented filtering by manually inputting a name parameter ["name"], the challenge now lies in adapting to the dynamic nature of searchColumnOnlyArray
, which may undergo modifications... The following code snippet illustrates the current method I am utilizing:
searchTable(term: any, data: any) {
let search = data.filter((indivitem: object) => {
if(indivitem["name"] === term) {
return indivitem;
}
}
console.log("searchResults", search);
}
Any suggestions or guidance on how to leverage the searchColumnOnlyArray
to exclusively search within the specified keys would be greatly appreciated. Tips on how to refine my filter function to search values based on the set keys in searchColumnOnlyArray
would also be valuable.