-1

I am facing an issue in react js, I want to get value of staff breaks from api data,

api data

staff: [
  {
    "id": "1",
    "full_name": "Barbershop",
    "breaks": [
      {
        "starttime": "10:00:00",
        "endtime": "12:00:00"
      },
      {
        "starttime": "12:30:00",
        "endtime": "1:00:00"
      }
    ]
  }
]

react component data

const getRowData = () => {
  return (
    this.state.staff &&
    this.state.staff.length &&
    this.state.staff.map(({ full_name,breaks }) => (
      <span className="more-space" key={full_name}>
        {id}
        {full_name}
        {breaks.start_date}   //this is not working
        {breaks.end_date}     //just show start_date or end_date
      </span>
    ))
  );
};

What should I change in my code? Can anyone help me?

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
adnan khan
  • 101
  • 1
  • 3
  • 7

3 Answers3

0

As per this :

staff: [
  {
    "id": "1",
    "full_name": "Barbershop",
    "breaks": [
      {
        "starttime": "10:00:00",
        "endtime": "12:00:00"
      },
      {
        "starttime": "12:30:00",
        "endtime": "1:00:00"
      }
    ]
  }
]

First : There is no start_date

Second : breaks is an array and not an object so {breaks.start_date} will not work, even if there was data with that key, you need to use index or loop over it again

I think what you need is something like this:

// {breaks.start_date}
// {breaks.end_date}
{ breaks.map(brk => <p>{brk.starttime}-{brk.endtime}</p> ) }
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
0
this.state.staff.map((item) => {
  <span className="more-space" key={item.full_name}>
    {item.id} 
    {item.full_name} 
    {item.breaks.map((value) => {
      { value.starttime }
      { value.endtime }
    })}
  </span>
});

Add another variable besides value if you need to access the key as below

this.state.staff.map((item, key) => {})
Akhil Chandran
  • 380
  • 3
  • 12
0

There is no start_date and end_date inside breaks array

Full working example

import React from 'react';

class Staffcomponent extends React.Component{
  constructor(props){
    super(props)
    this.staff= [
          {
            "id": "1",
            "full_name": "Barbershop",
            "breaks": [
              {
                "starttime": "10:00:00",
                "endtime": "12:00:00"
              },
              {
                "starttime": "12:30:00",
                "endtime": "1:00:00"
              }
            ]
          }
        ]
    this.state={staff:this.staff}
  }

  getRowData = () => {
       return (
             this.state.staff.map((item) => {
                  return (<span className="more-space" key={item.full_name}>
                            {item.id} 
                            {item.full_name} 
                             {item.breaks.map((break) => {
                                 return (<span>
                                        { break.starttime }
                                         { break.endtime }
                                         </span>)
                            })}

                         </span>)
                        })
                 );
};

  render(){
    return(
      <div>{this.getRowData()}</div>
    )
  }
}

function App() {
  return (
    <div className="App">
      <Staffcomponent/>
    </div>
  );
}

export default App;

jobayersozib
  • 401
  • 2
  • 7