I have the following data array that I wanna filter by company name/names. filter company name could be one or many.
data = [
{
"company":{
"name":"Company 1",
"symbol":"one"
},
"description":"test",
"status":"Pending"
},
{
"company":{
"name":"Company 2",
"symbol":"two"
},
"description":"test",
"status":"Pending"
},
{
"company":{
"name":"Company 3",
"symbol":"three"
},
"description":"test",
"status":"Pending"
}
]
filter = ["Company 1", "Company 3"]
Expected Result
[
{
"company":{
"name":"Company 1",
"symbol":"one"
},
"description":"test",
"status":"Pending"
},
{
"company":{
"name":"Company 3",
"symbol":"three"
},
"description":"test",
"status":"Pending"
}
]
Attempted the following
selectedCompaniesEvent() {
const fn = (arr, filters) => {
const filtersArr: any = Object.entries(filters);
return arr.filter((o, index) =>
filtersArr.every(([key, values]) =>
values.includes(o[index].company[key])
)
);
};
const result = fn(data, filter);
console.log(result);
}
I tried following the solution here, since my data array is nested it doesn't seem to work. any help will be appreciated!