4

Description

So I have an array like this:

const CategoriesList = [
    {nameNL:'bakkerij', 
     nameFR:'boulangerie', 
     nameEN:'bakery', 
     nameDE:'Bäckerei', 
     categories: [
        {nameNL:'bakkerij', nameFR:'boulangerie', nameEN:'bakery', nameDE:'Bäckerei'},
        {nameNL:'bier', nameFR:'la bière', nameEN:'beer', nameDE:'Bier'},
    ]},
]

And I can easily map all my items. However; inside that map function I want to loop between the categories array inside the categorieslist. I have tried to do this but apparently I can't use a for loop inside my map.

Question

How can I loop objects inside a map function.

What I have

 {CategoriesList.map(({nameNL, nameFR, nameEN, nameDE, categories}, index) => {
    return (
        <div key={index} className='flxRow'>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameNL}</div>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameFR}</div>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameEN}</div>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameDE}</div>
            {
                for (i = 0; i < categories.length; i++) { 
                    console.log(categories[i].name)
                }
            }
        </div>  
)})}
1
  • 1
    you use another map. Commented Dec 11, 2018 at 11:47

3 Answers 3

5
const CategoriesList = [
{nameNL:'bakkerij', 
 nameFR:'boulangerie', 
 nameEN:'bakery', 
 nameDE:'Bäckerei', 
 categories: [
    {nameNL:'bakkerij', nameFR:'boulangerie', nameEN:'bakery', nameDE:'Bäckerei'},
    {nameNL:'bier', nameFR:'la bière', nameEN:'beer', nameDE:'Bier'},
]},

]

const result = CategoriesList.map(item => {
 return item.categories.map(item2 => {
   return (
    console.log(item2.nameNL));
  })
 });

Working:

https://codepen.io/anildelhi/pen/YdPmvL?editors=1111

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

Comments

4

You can use another map:

 {CategoriesList.map(({nameNL, nameFR, nameEN, nameDE, categories}, index) => {
    return (
        <div key={index} className='flxRow'>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameNL}</div>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameFR}</div>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameEN}</div>
            <div className={['flxCell', 'flxCol2'].join(' ')}>{nameDE}</div>
            {
                categories.map(category => ...)
            }
        </div>  
)})}

I recommend reading about keys as well

1 Comment

That did it; thank you! & I understand what you mean about the keys part.
0
news API has JSON data like:-

{data: [
{
Description: "dummy description.",thumbnail_img_path: "images.jpg",video_path: "Z9xBo001urjkd"
},
{
Description: "dummy desc2.",thumbnail_img_path: "images-thumnail-1.jpg",video_path: "jklbtt2of8"
}]}
{news.map((item,i) => (   
                    
                    <div key={i}>
                    {
                        item.data.map((data, index) => (  //console.log(data.Description)) )                        
                        <div key={index}>
                            <div className="panel video-thumbnail" >
                            <img className="img-fluid"  src={data.thumbnail_img_path} alt="thum1" ytid={data.video_path} />
                            <div className="img-overlay">
                            <a href="#"><img src="../micro-assets/img/video-play.svg" alt="vplay" /></a>
                             </div>
                            </div>  
                        </div>
                        ))
                    }
                </div>                       
                ))                                  
        }

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.