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

107
Views
How to access value of data inside nested object in react

I am new to react and having problem trying to access data inside of the nested object. I am trying to create a app where the condition checks todays date with the date of the list and if the condition is true it returns the list.

The list looks like this :


const data = [{
    id: 1,
    name : 'Ashley',
    birthday : {
      year : 1998,
      month: 5,
      date : 22,
    },
    img : 'img'
  },
  {
    id: 2,
    name : 'Bill',
    birthday : {
      year : 1993,
      month: 2,
      date : 12,
    },
    img : 'img'
  },
];

I have made a date object with which the comparision will be done.

const today = new Date();
const year = today.getFullYear();
const month = today.getMonth()+1;
const date = today.getDate();

In the list, the birthday contains three other values and i can not access them using 'filter' method.

The rest of code are below :

function App() {
  const [people, setPeople] = useState(data)
  return (
    <section className = 'container'>
      <h1>{List.length} birthdays today</h1>
    <List people= {people}/>
      <button className="btn" onClick ={() => { setPeople([])}}>Clear All</button>
    </section>
  )
}
const List = ({people}) =>{
  return (
    <>
     { data.filter((person) => {
       const {id, name, birthday, img} = person;
       const age = year-person.birthday.year;
      if(person.birthday.month === month && person.birthday.date === date){
        return(
         <article className= 'person' key = {id}>
            <h2>{name}</h2>
            <p>{age} years old</p>
            <img src = {img} alt = "person"></img>
         </article>
       )}
       return (
         []
       )
     })} 
    </>
  )
}

Sorry about the long description.

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

0

The filter function takes a predicate as a parameter that returns true or false, then returns the elements for which the condition is true.

You are returning JSX/an empty array in the predicate, so I am assuming what you want is to actually map the elements to JSX.

Your code would be:

const List = ({people}) =>{
  return (
    <>
      {people.map((person) => {
        const {id, name, birthday, img} = person;
        const age = year - birthday.year;
        if(birthday.month === month && birthday.date === date){
          return (
            <article className='person' key={id}>
                <h2>{name}</h2>
                <p>{age} years old</p>
                <img src={img} alt="person"></img>
            </article>
          )
        }
        return null;
      })} 
    </>
  )
}

I have replaced data by people as it is the correct prop, and [] by null as [] is not valid JSX.

Alternatively, you can filter then map as follow:

const List = ({people}) =>{
  return (
    <>
      {people
        .filter((person) => person.birthday.month === month && person.birthday.date === date)
        .map((person) => {
          const {id, name, birthday, img} = person;
          const age = year - birthday.year;
          return (
            <article className='person' key={id}>
                <h2>{name}</h2>
                <p>{age} years old</p>
                <img src={img} alt="person"></img>
            </article>
          )
        })}
    </>
  )
}
about 4 years ago · Juan Pablo Isaza Report

0

You should be filtering based on the people being passed into List, not the data object.

const List = ({people}) =>{
  return (
    <>
      // instead of data
      { people.filter((person) => {
        const {id, name, birthday, img} = person;
        ...

That will allow you to access id, name, birthday, and img

about 4 years ago · Juan Pablo Isaza Report

0

You are passing props in curly brackets:

<List people= {people}/>

it should be passed like this:

<List people=people/>

and data.filter should be replaced with people.map.

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!