I possess an item with two fields filter1
and filter2
containing values in the form of arrays
let filter = {filter1:["mine","your"]: filter2:["C","D"]}
//values are not predetermined
The data is structured as an array of objects
let data = [
{ id:1, filter1:["mine"], filter2:["E","C"]},
{ id:2, filter1:["mine"], filter2:["E","C","F"]},
{ id:3, filter1:["your"], filter2:["C"]},
{ id:3, filter1:["your"], filter2:["D","C"]},
{ id:5, filter1:["other"], filter2:["F"]},
...
]
I need to sift through the objects that have either of the fields present in a specific key. For example, if filter
is
{filter1:["mine"]: filter2:["F","D"]}
, it will first look for any element of filter1 in the filter1 of the data object, and then search for any element of filter2 which is also found in the filter2 of the data object. It will return the object if any of them is discovered.
A few examples
Result for
{filter1:["mine"]: filter2:["F","D"]}
result = [
{ id:1, filter1:["mine"], filter2:["E","C"]}, //since filter1 "mine"
{ id:2, filter1:["mine"], filter2:["E","C","F"]}, //since filter1 "mine"
{ id:3, filter1:["your"], filter2:["D","C"]}, //since "F" and "D" from filter2, "D" is present
{ id:5, filter1:["other"], filter2:["F"]}, //since "F" from filter2 is found
]
Result for
{filter1:["your"]: filter2:["F","G"]}
result = [
{ id:2, filter1:["mine"], filter2:["E","C","F"]}, //since "F" from filter2 is present
{ id:3, filter1:["your"], filter2:["D","C"]}, //since filter1 is "your"
{ id:5, filter1:["other"], filter2:["F"]}, //since "F" from filter2 is present
]
Result for {filter1:[]: filter2:["D"]}
result = [
{ id:3, filter1:["your"], filter2:["D","C"]}, //since filter2 has "D"
]