I have an array of objects coming from backend as shown below.
let input = [
{month: "Dec", count: "45"},
{month: "Mar", count: "12"},
{month: "June", count: "5"}
]
The below variable dropdownValues
is the ones selected by user on frontend dropdown.
let dropdownValues = [
{key: 'Mar'},
{key: 'June'}
]
so considering user selected Mar
and June
in dropdown, we want the input
variable to only select those keys and compare it with month
property of input
and remove the other objects.
So the final output based on dropdownValues
should look like this
output = [
{month: "Mar", count: "12"},
{month: "June", count: "5"}
]
In order to achieve this result, i tried doing the following but it doesnot filter out Dec
month object.
let output = input?.filter((el) => {
return dropdownValues.some((f) => {
return f.key === el.month;
});
});
can someone let me know where i am going wrong with this.