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

169
Visualizações
How to retrieve a value already initialized in an associative array

I wish I didn't have to rewrite some things that could just be copied from a variable in an associative array created.

That is my code, but I got an error that says title is not defined :

const shopLocation = {
  "uccle-heros": {
    title: "Uccle Héros Defré",
    location: [4.334562, 50.043438],
    description: '<strong>'
      + title
      + '</strong>'
      + '<p>'
      + '<a id="test" '
      + 'href="http://www.mtpleasantdc.com/makeitmtpleasant" '
      + 'target="_blank" '
      + 'title="Opens in a new window">'
      + 'Make it Mount Pleasant</a>'
      + 'is a handmade and vintage market and afternoon of live '
      + 'entertainment and kids activities. 12:00-6:00 p.m.'
      + '</p>'
  }
};
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to use a getter:

const shopLocation = {
  "uccle-heros": {
    title: "Uccle Héros Defré",
    location: [4.334562, 50.043438],
    get description() {
      return '<strong>' + this.title + '</strong><p><a id="test" href="http://www.mtpleasantdc.com/makeitmtpleasant" target="_blank" title="Opens in a new window">Make it Mount Pleasant</a> is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>'
    }
  }
};

console.log(shopLocation)

about 4 years ago · Juan Pablo Isaza Relatório

0

In addition to @Spectric's answer, use interpolated strings. Much more readable:

const shopLocation = {
  "uccle-heros": {
    title: "Uccle Héros Defré",
    location: [4.334562, 50.043438],
    get description() {
      return `
        <strong>${this.title}</strong>
        <p>
          <a
            id="test"
            href="http://www.mtpleasantdc.com/makeitmtpleasant"
            target="_blank"
            title="Opens in a new window"
          >
            Make it Mount Pleasant
          </a>
          is a handmade and vintage market and afternoon
          of live entertainment and kids activities.
          12:00-6:00 p.m.
        </p>`
    }
  }
};

console.log(shopLocation)
about 4 years ago · Juan Pablo Isaza Relatório

0

Ok first of this is called an object in javascript

secondly in order to reference something nested in a nested object like this you have to call it as such shopLocation['uccle-heros'].title

about 4 years ago · Juan Pablo Isaza Relatório
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