PrStandup
PrStandup

Reputation: 343

How to access an element on array obj

I want to add an a href link <a href=" ">Go Back</a>

I want to bind the value from rows, which is an array:

rows: [["href": "href.com", "id"=>"10"], ["href":"href.com", "id"=>"20"]]

href value its the same on each array, so how can I get href without doing v-for="row in rows"

Upvotes: 0

Views: 102

Answers (2)

akuiper
akuiper

Reputation: 214927

Taking your array as [{"href": "href.com", "id":"10"}, {"href":"href.com", "id":"20"}] and all subarrays have the same href, you can extract the first element in the rows with rows[0] and then access the href key as a normal javascript object. In the template, you can write <a :href="rows[0]['href']">Go Back</a>.

Upvotes: 1

twenty
twenty

Reputation: 77

You currently have an array within an array. I assume your intention was to have an object within an array, like this:

rows: [{"href": "href.com", "id": "10"}, {"href":"href.com", "id": "20"}]

In which case you can do what Psidom suggested and access the object within the rows array with rows[0]['href'] or do a v-for within rows like this:

<template v-for="row in rows" :key="row.id">
  <a :href="row.href">Link</a>
</template>

Upvotes: 2

Related Questions