0

I have smth like this

const activeLang = 'pl'
 
const uniqueCategories = products.map((product) => {
      return product.category_pl
    })

How do I make this the same but with using template string. It should be smth like this

product.category`_${activeLang}

But it doesn't work. Any help?

Filippo854
  • 149
  • 9
  • 1
    Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Heretic Monkey Nov 11 '20 at 19:32

1 Answers1

0

I think I miss understood you question any way check this solution

const activeLang = 'pl'
const uniqueCategories = products.map((product) => {
      return product[`category_${activeLang}`]
    })

Explanation : using [] instead of . when we call a property of an object when the property has to be evaluated first.

for more check this answer : JavaScript property access: dot notation vs. brackets?