2

I am trying to use map() and display json data but it throws error see screenshot below.I am mapping each JSON object ans displaying it out using showslots() function.

data.js:

const data = {
        "template_type": "slot_picker",
        "selection_color": "#000000",
        "secondary_color": "#808080",
        "title": "Available Slots for Dr. Sumit",
        "available_slots": [
          {
            "date": "Wed, Dec 06",
            "date_slots": [

            ]
          },
          {
            "date": "Thu, Dec 07",
            "date_slots": [

            ]
          },
          {
            "date": "Fri, Dec 08",
            "date_slots": [

            ]
          },
          {
            "date": "Sat, Dec 09",
            "date_slots": [

            ]
          },
          {
            "date": "Today",
            "date_slots": [
              {
                "hour": "8",
                "hour_slots": [
                  {
                    "08:10 AM": "slotId001"
                  },
                  {
                    "08:50 AM": "slotId005"
                  }
                ]
              },
              {
                "hour": "3",
                "hour_slots": [
                  {
                    "03:00 PM": "slotId005"
                  },
                  {
                    "03:30 PM": "slotId007"
                  }
                ]
              }
            ]
          },
          {
            "date": "Tomorrow",
            "date_slots": [

            ]
          },
          {
            "date": "Wed, Dec 13",
            "date_slots": [
              {
                "hour": "4",
                "hour_slots": [
                  {
                    "04:30 PM": "slotId105"
                  },
                  {
                    "04:50 PM": "slotId106"
                  }
                ]
              },
              {
                "hour": "5",
                "hour_slots": [
                  {
                    "05:30 PM": "slotId202"
                  },
                  {
                    "05:45 PM": "slotId208"
                  }
                ]
              }
            ]
          }
        ]
      };

 export default data;

In datapicker.js :

  import React, { Component } from 'react';
import data from './data';
import './datepicker.css';

class DatePicker extends Component {

  constructor(props){
    super(props);
     this.state = {
        counter:0,
        issubmitted:false
     };
   }

  increment(){
    if(this.state.counter < 6){
      this.setState(prevState => ({counter: prevState.counter + 1}))
    }
  }

  decrement(){
    if(this.state.counter > 0){
      this.setState(prevState => ({counter: prevState.counter-1}))
   }
  }

  formsubmitted(){
    this.setState({
        issubmitted:true
    })
  }

  showslots(){
        if(data.available_slots[this.state.counter].date_slots.length === 0){
            return(
                <p>No slots</p>
                )
        }else {
            return(

                    data.available_slots[this.state.counter].date_slots.map(obj =>{

                        return (
                          <div id="slotinfo">
                           <div> <p>Hour : {obj.hour}</p> </div>
                            <div><p>slot: {obj.hour_slots[0]}</p></div>
                            <div><p>slot: {obj.hour_slots[1]}</p></div>

                          </div>
                        );
                    })
                )
        }

  }

  render() {
    return (
      <div>

        <div id="center">
        <div className="title">
            <p>Pick a Date</p>
        </div>
        <div className="increment">
          <button type="button" className="btn btn-success" id="plus" onClick={this.increment.bind(this)}>+</button>
        </div>
        <div className="display">
          <input type="text" id="date" value={data.available_slots[this.state.counter].date}/>
        </div>
        <div className="decrement">
          <button type="button" className="btn btn-danger" id="minus" onClick={this.decrement.bind(this)}>-</button> 
        </div>
        <div className="status">
          { data.available_slots[this.state.counter].date_slots.length === 0 ? 
          <p>No slots available for today</p> : <p>Slots available for today</p> }
        </div>
        <div className="submit">
          <button type="button" className="btn btn-primary" id="submit" onClick={this.formsubmitted.bind(this)}>Book Slot</button> 
        </div>
      </div>

      <div>
        {this.state.issubmitted ? this.showslots() : ''}
      </div>


      </div>
    );
  }
}

export default DatePicker;

enter image description here

5
  • you'll get a type error like this because map function on iterates arrays try pushing this in an array Commented Feb 24, 2018 at 15:00
  • @YahyaAhmed I want to display "08:10 AM": "slotId001" how can I do that using above code? Commented Feb 24, 2018 at 15:02
  • The error is related to how it's being rendered. Could you edit the question to show your render function? Commented Feb 24, 2018 at 15:02
  • for(var x in object) iterates through objects you can use that. Commented Feb 24, 2018 at 15:07
  • @LazyElephant Check the question I have edited. Commented Feb 24, 2018 at 15:10

2 Answers 2

2

Write it like this:

hour_slots.map((el,i) => {
   let key = Object.keys(el)[0];
   return <div key={i}>{key} : {el[key]}</div>
})

Full Code:

data.available_slots[this.state.counter].date_slots.map((obj,j) => {
    return (
        <div id="slotinfo" key={j}>
            <div> <p>Hour : {obj.hour}</p> </div>
            {
                obj.hour_slots.map((el,i) => {
                    let key = Object.keys(el)[0];
                    return <div key={i}>{key} : {el[key]}</div>
                })
            }
        </div>
    );
})

Working Snippet:

const data = {
    "available_slots": [
        {
            "date": "Today",
            "date_slots": [
              	{
                	"hour": "8",
                	"hour_slots": [
		                {
		                    "08:10 AM": "slotId001"
		                },
		                {
		                    "08:50 AM": "slotId005"
		                }
            		]
        		},
        		{
		            "hour": "3",
		            "hour_slots": [
		                {
		                    "03:00 PM": "slotId005"
		                },
		                {
		                    "03:30 PM": "slotId007"
		                }
		            ]
       			}
    		]
        },
    ]
}

const App = props => {
	return <div>
		{data.available_slots[0].date_slots.map(obj => {
		    return (
		      	<div id="slotinfo">
			       	<div> <p>Hour : {obj.hour}</p> </div>
			        {
			        	obj.hour_slots.map((el,i) => {
			       			let key = Object.keys(el)[0];
			       			return <div key={i}>{key} : {el[key]}</div>
			    		})
			    	}
		      	</div>
		    );
		})}
	</div>
}

ReactDOM.render(<App />, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app' />

Sign up to request clarification or add additional context in comments.

8 Comments

Nothing is displayed when slot is available and when no slot then it shows no slot why so ?
@stonerock have you tried the full code part, its not showing?
I just inserted full code answer inside else {} but still doesn't work ?
any idea how to display that JSON data of slot hour?
check the working snippet, its working i think?? using data which has the hours_slot.
|
1

consider given data,

"hour_slots": [
                  {
                    "08:10 AM": "slotId001"
                  },
                  {
                    "08:50 AM": "slotId005"
                  }
                ]

When you say obj.hour_slots[0],it gives an object

{ "08:10 AM": "slotId001" }

which react jsx not understand ant not able to render it.

That's why you getting such error.You need to access the individual property to display slotId001.

EDIT :

As per comment given below,in order to display "08:10 AM": "slotId001",

e.g. for first element.

`${ Object.keys(obj.hour_slots[0])[0] } : ${data[Object.keys(obj.hour_slots[0])[0]] }`

2 Comments

I want to display "08:10 AM": "slotId001" how can I do that ?
How can I display "08:10 AM": "slotId001" using map() I am able to display hour:8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.