0

I have the following object with a "multimedia" array:

nytimes api


I only need one of the urls, but no idea how ot get it

    <div
          class="card mb-3"
          style="max-width: 540px;"
          v-bind:key="news.artices"
          v-for="news in newsList" >
    
          <div class="row no-gutters">
            <div class="col-md-8">
              <div class="card-body">
    
               //this doesn't work
                <p v-for="image in news.multimedia" v-bind:key="image">
                    {{image[0].url}} 
                </p>
    
               //this works, but shows all
                <p v-for="image in news.multimedia" v-bind:key="image">
                    {{image.url}} 
                </p>
    
                <h5 class="card-title">{{news.title}}</h5>               
              </div>
            </div>
          </div>
        </div>

2 Answers 2

4

If you want to display only one element of the multimedia array, then you don't need to use v-for unnecessarily. You can simply use:

<p v-bind:key="image" v-if="news.multimedia && news.multimedia.length">
   {{ news.multimedia[0].url }} 
</p>

Also, you can use v-if to make sure this div is only rendered if news.multimedia has a valid value and it not an empty array.

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

Comments

1

It looks like multimedia is an array of objects, so you could do: news.multimedia[0].url.

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.