0

I have a "photos" array of "photo" object. Each "photo" object has a property value for the image url.

I try to iterate over the "photos" array with map(), in this way

photos.map((photo, index) => {
    console.log(photo)
    console.log(photo.value)
    return (
        <img
            key={index}
            src={photo.value}
            className={index === active ? 'active' : ''}
            alt="thumbnail"
        />
    )
})

For some reason that I'm completely missing, photo.value is not available.


This is what those two console.log print out:

console.log output

It seems the object is available, but then the "value" property returns undefined.
This was supposed to be very simple but I'm completely stuck at this point.



How can this be possible? What am I missing here?

Thanks

3
  • 6
    Looks like it's photo.photo.value, you print the photo object, but that itself is an object with a "photo" property Commented Feb 6, 2019 at 11:19
  • 1
    Like pointed out by @Jayce444, the name of a callback value has no relation to the object,. To save any confusion, it might be best to rename your photo callback parameter to say item, then you would have item.photo.value, and that would just make more sense, than photo.photo. Alternatively you could destructure the photo param,.. photos.map(({photo}, index) => Commented Feb 6, 2019 at 11:24
  • really don't know how did I miss this... Thank you! Commented Feb 6, 2019 at 14:23

1 Answer 1

1

access value like this photo.photo.value

photos.map((photo, index) => {
    console.log(photo)
    console.log(photo.photo.value)
    return (
        <img
            key={index}
            src={photo.photo.value}
            className={index === active ? 'active' : ''}
            alt="thumbnail"
        />
    )
})
Sign up to request clarification or add additional context in comments.

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.