Skip to content

How do I make this with template string?

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?

Answer

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?