0

I have a hypothetical JSON file that reads like so:

{
  "main": [
    { 
      "dish": "steak",
      "side": [
         {"platter": "yogurt"},
         {"platter": "popcorn"}
      ]
    },
    { 
      "dish": "fish",
      "side": [
        {"platter": "salad"}
      ]
    }
  ]
}

I am currently having problems understanding how to run a for loop on this data with ReactJS. The rest of my code is organised as follows:

getInitialState:function(){
  return {
           fulldata: {
             main:[
               {
                 side:[]
               }
             ]
           }
         }
},

componentDidMount:function(){
  var self = this;
  $.getJSON('https://yooarel.fakewebsite', function(resultes){
    self.setState({fulldata: resultes});
  }); 
},

rundmc:function(){
  return (<ul>
      {
        this.state.fulldata.main.map(function(m, i){
           return m.side.map(function(make, o){
             return <li key={o}>{make.platter}</li>
           }) 
        })
      }
    </ul>)
},
render:function(){
  var self = this;
  return (<div>
    <ul>
      {this.state.fulldata.main.map(function(m, i){
        <li key={i}>
          <span>{m.dish}</span>
          {self.rundmc()}
        </li>
      })}
    </ul>
  </div>)
}

I am attempting to place a <ul li> block that lists out the different types of dishes I have. And then, for each dish under my li block, I am also attempting to place a ul li block that lists out the different platter under each dish. This is what I am trying to accomplish under my rundmc() function.

My code, as is, shows all platters under both of my ul ul li blocks. I am not sure how to output only the appropriate platter for each dish.

3 Answers 3

2

You are iterating all the data in rundmc function as well, you have to pass the current dish as a parameter and iterate only the side array

rundmc:function(dish){
  return (<ul>
      {
        dish.side.map(function(make, o){
          return <li key={o}>{make.platter}</li>
        })
      }
    </ul>)
},
render:function(){
  var self = this;
  return (<div>
    <ul>
      {this.state.fulldata.main.map(function(m, i){
        <li key={i}>
          <span>{m.dish}</span>
          {self.rundmc(m)}
        </li>
      })}
    </ul>
  </div>)
}
Sign up to request clarification or add additional context in comments.

Comments

2

I'd suggest to not use a method for this, as it's very simple to nest the maps, also doesn't require React to create new functions for each child every time it renders. Here's an example:

const json = {
	"main": [{
		"dish": "steak",
		"side": [{
			"platter": "yogurt"
		}, {
			"platter": "popcorn"
		}]
	}, {
		"dish": "fish",
		"side": [{
			"platter": "salad"
		}]
	}]
}

class Example extends React.Component {
  
  render() {
    return(
      <ul>{json.main.map((main, i) => (
        <li key={i}>
          {main.dish}
          <ul>{main.side.map(side => <li>{side.platter}</li>)}</ul>
        </li>))}
      </ul>
    );
  }
}

ReactDOM.render(<Example/>, document.getElementById('View'));
<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="View"></div>

Comments

1

Maybe you are not returning the value in the map function?

{this.state.fulldata.main.map(function(m, i){
  return (
    <li key={i}>
      <span>{m.dish}</span>
      {self.rundmc()}
    </li>
  )
})}

Edit: Didn't saw the rundmc method. As diedu showed, you are mapping again the same array instead of passing the actual value you want to map inside.

Comments

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.