0

I wanted to display following json data in nested react components .For example a list title with sub list I have tried using for loops but for some reason the component doesn't displays

Choose Your topping 1

  • Regurlar
  • Stuffed
  • Choose Your topping 2
  • Garllic
  • chilli
  • Structure of Json Data
    let MenuContents = [{
               Title:'Choose one',
               List:{
                 Option1:'Regular Base',
                 Option2:'Stuffed Crust',
                 Option3: 'Thick Base',
                 Option4: 'Thin Base'
               }
            },
            {
               Title:'Choose one',
               List:{
                 Option1:'Garlic Dip',
                 Option2:'Chilli Dip',
                 Option3: 'BBq Dip',
                 Option4: 'Mayo'
               }
            },
            {
               Title:'Choose one',
               List:{
                 Option1:'Tomato Base',
                 Option2:'BBq Base',
                 Option3: 'Garlic',
                 Option4: 'Mayo'
               }
           }]
    
    
    
     
        MenuContents.map((x) => {
        return (
          <div>
            <div className="AddoneTitle">
              <li>
                <h3>{x.Title}</h3>
              </li>
            </div>
            <div className='Choose'>
             {
              Object
              .keys(x.List)
              .forEach(function(key) {
               return( <li>x.List[key]</li>)
              })
             }
          </div>
        </div>
        )
       });
    
    1
    • use map instead of forEach Commented Jul 14, 2020 at 15:28

    2 Answers 2

    2

    This should work, forEach doesn't return an array of list items as map does, also you were missing {} inside li

              MenuContents.map((x) => {
              return (
                <div>
                  <div className="AddoneTitle">
                    <li>
                      <h3>{x.Title}</h3>
                    </li>
                  </div>
                  <div className='Choose'>
                   {
                    Object
                    .keys(x.List)
                    .map(function(key) {
                     return( <li>{x.List[key]}</li>)
                    })
                   }
                </div>
              </div>);
             })
    
    Sign up to request clarification or add additional context in comments.

    Comments

    1

    Use

    {
        x.List.map(record => {
            return (
                <li key={Object.keys(record)}>
                    {Object.values(record)}
                </li>
            )
        })
    }
    

    instead of:

     {
      Object.keys(x.List).forEach(function(key) {
    
    
       return( <li>x.List[key]</li>)
      
      })
      
     }
    

    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.