I am trying to filter a object based on an another array.
data = {
'TypeA': ['A1', 'A2', 'A3', 'A4'],
'TypeB': ['B1', 'B2', 'B3', 'B4'],
'TypeC': ['C1', 'C2', 'C3', 'C4'],
}
array = ['TypeA', 'TypeB']
I want to get a new array with the values of both ‘TypeA’ and ‘TypeB’ like [‘A1’, ‘A2’, ‘A3’, ‘A4’, ‘B1’, ‘B2’, ‘B3’, ‘B4’] and remove duplicates if any also.
so far i have tried,
tTmp_Arr = data.filter(tObj => array.includes(tObj))
Any help is much appreciated.
Thanks in advance.
Thanks,
3
Answers
You can’t use filter on objects. You can get the result by flatmapping and then filtering as:
Here’s another working example using
array.reduce()
with afor..of
loop:Here example using only for..loops