0

I have following scenario where I need loop through a data set and return multiple post. Each post has a title and few images attached to it. I am able to loop through all the data, but its returning the same title multiple times instead of just once.

Desired output:

My title img 1 img 2

Current Output:

My title image1 My title image 2

JS:

const mappedFlickr = this.state.articles.map((flickr, i) => { 
        return ( 
          flickr.fields.featuredImage.map((st, i) => { 
             return ( 
                <div>
                <span>{flickr.fields.title}</span>
                <img src={st.fields.file.url +'?w=300&h=300'} />
                </div>
              )
          })
        )
     })

return (
      <section>
         { mappedFlickr }
      </section>
    ) 
1
  • Yes, flickr.fields.title will be the same title regardless what image you have in the loop. Why don't you move the <span> out of the featuredImage.map, right in the <section>? Commented Jun 27, 2017 at 9:33

2 Answers 2

1

Move inner map inside div

const mappedFlickr = this.state.articles.map((flickr, i) => { 
        return ( 
           <div key={i}>
              <span>{flickr.fields.title}</span>
              {
               flickr.fields.featuredImage.map((st, j) => 
                 (<img key={j} src={st.fields.file.url +'?w=300&h=300'} />)
               )
              }

            </div>
        )
     })
Sign up to request clarification or add additional context in comments.

Comments

0

Try moving <span>{flickr.fields.title}</span> outside of the inner loop

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.