Get only one value from an array in react js

I have an array of images "[ {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125556.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125556.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125556.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125607.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125556.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125607.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125556.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125607.jpg"}, {"imgUri":"osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125681.jpg"}]" but i only want to get the first object of this array to display as cover image in react

Hello there,

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

1 Like

Thanks @Sky020, so concerning the example i tried

                const imgs = JSON.parse(post.images)
                const imgsLength = imgs.length

                    const img = post.images[0];
                    const coverUrl = `http://localhost:5000/images/${img.imgUri}`
                    const cover = <img src={coverUrl} />
                    console.log(coverUrl);

am getting undefined when am trying to log coverUrl.

It is difficult to be able to know what is exactly wrong, without seeing the exact data. The data you have posted in the code above is neither a JS Array, nor a JS String, nor JSON. So, let us assume the data is JSON: You need to parse it, as you have here:

const imgs = JSON.parse(post.images)

But, then you do not parse it here:

const img = post.images[0];

So, it is not a valid JS object.

Hope this helps

1 Like

I’ve solved this issue . I changed the array data from the last one to

const images = " [ "osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125556.jpg, "osbook2020-f51d7ad8-d8ce-4f9e-9bec-9b56f11f57ea1590130125556.jpg"]"

so not when I call

 const imgs = JSON.parse(post.images)
 const img = imgs[0];
console.log(img)

I get the first value of the array