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

180
Views
Accessing Hierarchy Data in an Object Javascript

I need to access the id value in a Javascript object to display a simple message that the user has membership if the id value equals a specific value.

I'm getting Uncaught Type Error not defined 'id' message

In the console it's displayed as

subscriptions: Array(1)
     0: // COMMENT Don't know what this is
       autoRenew: false
       canRenew: false
       expiryDate: "2022-10-26T00:00:00"
       membership:
                 id: "819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL"

I'm assuming equivalent JSON is something like:

subscriptions {
    0
           {
       membership: {
           id: "819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL"
}
}
}

My Javascript Code

const userObj3 = userObj['subscriptions']['0']['membership']['id'];

if (userObj3 = "819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL") {
  greeting = "You have membership";
}


about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your subscriptions are an array, also your comparison inside the if is incorrect and should be == or ===, you could also use dot annotation to traverse the object instead of using brackets on every key.

Using the index as a string instead of a number isn't really wrong, it's just not a good practice.

You might want to think about looping through the subscriptions instead of using the direct index just in case there's multiple subscriptions. But that just depends on how you structure your data and is kinda up to you.

const userObj = {
  subscriptions: [
    {
      autoRenew: false,
      canRenew: false,
      expiryDate: '2022-10-26T00:00:00',
      membership: {
        id: '819AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL'
      }
    },
    {
      autoRenew: true,
      canRenew: false,
      expiryDate: '2022-10-26T00:00:00',
      membership: {
        id: '201AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL'
      }
    }
  ]
};

let greeting = "You're not a member";

userObj.subscriptions.forEach((sub) => {
  if (sub.membership.id === '201AGBHDRLQHNHPHKKMPKLGPMDRDTDMVL') {
    greeting = "You're a member";
  }
});

console.log(greeting);
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!