1

I am new to vue.js so this might be an easy question, but I unfortunatelly didnt come to a result on my own.

So this is the basis of my template:

<template v-for="page in $props.data.pageInfo" >
  <div class="overviewItem" :key="page.uid" :data-cat=" **looping through the object to get both cat_id's ** ">
    <div v-for="(cat, index) in page.categories" :key="index" >
      <p v-if="index === 0" class="subtitle">
        {{ cat.title }}
      </p>
    </div>
  </div>
</template>

{{ page.categories }} has the following values at the first .overviewItem:

[
{
  "cat_id": 1,
  "title": "Kategorie 1"
},
{
  "cat_id": 2,
  "title": "Kategorie 2"
}
]

The loop to get the subtitle works perfectly fine, but I cannot find any option on how to loop through this object so that i get the two values of data-cat attribute.

2
  • 1
    Remove the v-if on the <p> which is only allowing the first item to be rendered and put data-cat on the loop <div> Commented Dec 30, 2020 at 15:35
  • I need the data-cat to be outside (on the .overviewItem). Inside I do actually only want the first title to be displayed, that is working fine for me. Commented Dec 30, 2020 at 15:47

1 Answer 1

1

Use .map to generate a new array of just the cat_id and then join them together with a space in between:

<div
   class="overviewItem"
   :key="page.uid"
   :data-cat="page.categories.map(c => c.cat_id).join(' ')"
>
...
</div>
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.