0

I have a 'const' that returns an array of objects. Inside each of these objects is a key for 'image' and inside that object is a value for 'url' as shown below.


const images =[
    {
        "image": {
            "url": "imageUrlHere"
        }
    },
        {
            "image": {
                "url": "imageUrlHere"
            }
        },
        {
            "image": {
                "url": "imageUrlHere"
            }
        }
    ]

I can access each of these values individually by specifying an array index as seen below:

const imageUrls = images[0].image.url;

But what I need, is to create a new array from all of the image.url values, so I can output all of the images on my page.

Thanks!

Connor
  • 21
  • 5

1 Answers1

1

You can do this with a simple map.

const images =[
    {
        "image": {
            "url": "imageUrlHere"
        }
    },
        {
            "image": {
                "url": "imageUrlHere"
            }
        },
        {
            "image": {
                "url": "imageUrlHere"
            }
        }
    ]
    
const urls = images.map(o => o.image.url)
console.log(urls)
Mina
  • 14,386
  • 3
  • 13
  • 26