Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Not getting nested property value

I have a function to convert units to my native language. My function -

const toCorrectUnitLanguage = (unit, langLocale = 'en') => {
  const savedUnits = {
    sec: { en: 'Sec', bn: 'সেকেন্ড' },
    min: { en: 'Min', bn: 'মিনিট' },
    hour: { en: 'Hour', bn: 'ঘন্টা' },
  }
  if (savedUnits[unit.toLowerCase()]) {
    return savedUnits[unit.toLowerCase()][langLocale];
  }
  return unit;
}

console.log(toCorrectUnitLanguage('sec'))
console.log(toCorrectUnitLanguage('sec', 'en'))
console.log(toCorrectUnitLanguage('sec', 'bn'))

I am getting the correct object when I am logging savedUnits[unit.toLowerCase()]. I am also getting the value I want if I hard code savedUnits[unit.toLowerCase()]["en"]

But I am getting undefined when I am returning savedUnits[unit.toLowerCase()][langLocale]

Any suggestions? Thanks in advance.

Edit: Calling the function like this -

toCorrectUnitLanguage('sec',langLocale)

My node version is 13.14.0. Could that be an issue?

Edit 2: My console.log

if (savedUnits[unit.toLowerCase()]) {
  console.log(savedUnits[unit.toLowerCase()]); //{en: 'Sec', bn: 'সেকেন্ড'}
  console.log(savedUnits[unit.toLowerCase()][langLocale]); //undefined
  return savedUnits[`${unit.toLowerCase()}`][langLocale];
}
about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda