Reputation: 23
I have one array of objects containing the menu for my multi select dropdown
const menu = [
{
label: "test 1",
value: "test1",
},
{
label: "test 2",
value: "test2",
},
// so on
]
I have another array of object that is storing selected multiple values from the menu:
const values = [
{
label: "test 1",
value: "test1",
}
// It can contain more than one values since the dropdown is multiselect
]
Remember that menu array contains all the options that I am displaying in my dropdown. Now, what I want is to filter out the selected values from the dropdown. For this purpose, I will need to filter out info from menu array of objects and for that filtration, I will be using values array of objects so that the objects inside the two arrays can be compared and filter out from my menu array of objects.
I have tried numerous JS methods but nothing is working in my case. I cannot use includes method because it compares values by reference and not by value. This is the reason why my condition fails since JS considers both of the objects to be different (their address is different), although they have the same value.
Looking forward to a solution.
Upvotes: 0
Views: 56
Reputation: 19947
selectedValues = values.map(item => item.value)
menu.filter(item => selectedValues.includes(item.value))
Upvotes: 1