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

97
Views
How to access key in Nested object in an array within a JSON file

var data = [
  {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
  }];

console.log(data.items[0].name === 'foo');

I am trying to access the key and checking if the value are same then return true or false, I am getting an error while accessing it.

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

0

Data is an array. It should be data[0].items[0].name

var data = [
  {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
  }];

console.log(data[0].items[0].name === 'foo');

about 4 years ago · Juan Pablo Isaza Report

0

According to your data it should be like this

data[0].items[0].name === 'foo'
about 4 years ago · Juan Pablo Isaza Report

0

Its data[0].items[0].name

  • data is an array.
  • items node in each node of data is also an array.

To access the zeroth node of data you have to use data[0] just same as you did with items

var data = [
  {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
  }];

console.log(data[0].items[0].name === 'foo');

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!