Looking to refine an array based on a variable number of items in another array. Consider the initial array:
var toBeFiltered = [
{name:"A", parentId: 0},
{name: "B", parentId: 3},
{name: "C", parentId: 0},
{name: "D", parentId: 1},
...
]
I want to filter out all elements for which parentId
matches any value from another array (let's call it filtering = [3,1,0]
, although the length can vary).
How would one create a dynamic filtering expression based on the contents of the filtering
array? In this scenario, the resulting expression could look like:
function(d){return d.parentId == 3 || d.parentId == 1 || d.parentId == 0;}
Is there an efficient method to achieve this? Perhaps combining boolean expressions somehow?