-3

I have a key value pair array in js

var tabList = {0:'#description', 1:'#media', 2:'#attributes', 3:'#calendar', 4:'#pricing'}

I'm using the keys to get the values in my code

ie. tabList[2] returns #attributes

I thought I could do the same in reverse to get the key

tabList[#media] and have it return 1

But this doesn't work

How can I fetch the key with only the value as input?

xslibx
  • 4,249
  • 9
  • 35
  • 52

1 Answers1

1

There are plenty of solutions here Swap key with value JSON

I will flip key with values 1st

var tabList = {0:'#description', 1:'#media', 2:'#attributes', 3:'#calendar', 4:'#pricing'}
let flipped=Object.assign({}, ...Object.entries(tabList).map(([k,v]) => ({ [v]: k })))
console.log(flipped);
console.log(flipped['#description']);
sumit
  • 15,003
  • 12
  • 69
  • 110