Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!