I have an object:
a = {
"1": "abc",
"2": "def",
"3": "ghi",
"4": "jkl"
}
and an array:
b = ['abc','ghi']
I want to get keys from the objects which values are in an array and place them in another array, so expected output is: ['1', '3']
.
I have no idea how to filter object properties. I tried mapping an array and get values but I get undefined.
const result = b.map(v => a[v])
You could use Object.keys(a).filter(...)
, and only return true if a[key]
is in the b
array.
Use filter()
on the Object.keys()
:
const a = {"1": "abc", "2": "def", "3": "ghi", "4": "jkl"};
const b = ['abc','ghi']
const c = Object.keys(a).filter(k => b.includes(a[k]));
console.log(c);