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

229
Views
JSON - Uncaught TypeError: Cannot read properties of undefined (reading '0')

I'm getting this error "Uncaught TypeError: Cannot read properties of undefined (reading '0')" when I try to return the .Name of my JSON.

Here's my javascript (The console.log is for testing):

var roomsCodes = [
    ['232','DDSB'],
    ['232','DDMB'],
    ['232','SJJB'],
    ['232','SJJS'],
];

var jsonMin = 
{
    "DDSB": {
        "Name": "Doble Estándar",
    },
    "DDMB": {
        "Name": "Doble con vista mar",
    },    
    "SJJB": {
        "Name": "Jr. Suite",
    },
    "SJJS": {
        "Name": "Jr. Suite Superior",
    },
}

for(var p = 0; p < roomsCodes.length; p++){
    if(232 == roomsCodes[p][0]){
        console.log(jsonMin.roomsCodes[p][1].Name);
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can fix it by using [] to access to the object, like this:

var roomsCodes = [
  ["232", "DDSB"],
  ["232", "DDMB"],
  ["232", "SJJB"],
  ["232", "SJJS"]
];

var jsonMin = {
  DDSB: {
    Name: "Doble Estándar"
  },
  DDMB: {
    Name: "Doble con vista mar"
  },
  SJJB: {
    Name: "Jr. Suite"
  },
  SJJS: {
    Name: "Jr. Suite Superior"
  }
};

for (var p = 0; p < roomsCodes.length; ++p) {
  if (232 == roomsCodes[p][0]) {
    console.log(jsonMin[roomsCodes[p][1]].Name);
  }
}

When you want to access to a property using a dynamic variable you need to use square brackets otherwise you can use the dot.

about 4 years ago · Juan Pablo Isaza Report

0

var roomsCodes = [
    ['232','DDSB'],
    ['232','DDMB'],
    ['232','SJJB'],
    ['232','SJJS'],
];

var jsonMin = 
{
    "DDSB": {
        Name: "Doble Estándar",
    },
    "DDMB": {
        Name: "Doble con vista mar",
    },    
    "SJJB": {
        Name: "Jr. Suite",
    },
   "SJJS": {
        Name: "Jr. Suite Superior",
    },
}

for(var p = 0; p < roomsCodes.length; p++){
    if(232 == roomsCodes[p][0]){
      
      const item = roomsCodes[p][1]
      
     
      
      console.log(jsonMin[item].Name)
      
    }
}

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!