-2

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])
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
silviaa11
  • 89
  • 9

2 Answers2

2

You could use Object.keys(a).filter(...), and only return true if a[key] is in the b array.

TKoL
  • 13,158
  • 3
  • 39
  • 73
1

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);
0stone0
  • 34,288
  • 4
  • 39
  • 64