I need to filter data in an object array based on a predicate. The tricky part is constructing the predicate, which should be similar to building a query using a Map data structure.
Here is an example of my objects array:
var data = [
{"id": 1, "name": "bob", "address": "123 abc", "status": "active"},
{"id": 2, "name": "henry", "address": "123 def", "status": "inactive"},
{"id": 3, "name": "henry", "address": "123 hij", "status": "active"}
];
And here is the Map structure that should be used to construct the predicate:
var map = new Map<string, any>();
map.set("name", ["henry", "bob"]);
map.set("status", "active");
The desired output for this scenario would be:
[{"id" : 3, "name": "henry", "address": "123 hij", "status": "active"}]
Now, I'm struggling with how to write the data.filter((item) => {...}) function dynamically to build the predicate and filter the results without hardcoding property names. Additionally, the value could be a string, array, or a number. The hypothetical predicate should look like this:
data.filter((item) =>
{ item["name"] IS IN map.get("name").values[]
&& item["status"] IS EQUAL TO map.get("status").value)});
I'm finding it difficult to convert the Map object into a usable predicate for filtering. Any tips or guidance on this matter would be greatly appreciated.