0

Let's say I have this list of objects in Vue.JS

data () {
return{
  examples: [
    {
      exampleID: 5,
      exampleText: 'foo'
    },
    {
      exampleID: 3,
      exampleText: 'bar'
    }
  ]
}
}

Now let's say I want to display the object with the exampleID of 3 in an element i created before

   <Task 
  v-for="example in examples"
  :key="example.exampleID"
  :example="example"
/>

I want to display everything, that is in the object (the ID and the text)

Task component :

<template>
    <div class="exercise">
        <div class="exercise-id">
            <h1>ID NUMBER: {{ example.exampleID }}</h1>
        </div>
        <div class="exercise-task">
            <h2>{{ example.exampleText}}</h2>
        </div>
    </div>
</template>
 
<script>
 
export default {
  name: 'Task',
  props: ['example']
}
</script>
12
  • your question is not clear enough, please share the Task component code Commented Nov 11, 2020 at 16:56
  • The Task component is in an external file in the components folder. Here's the code pastebin.com/T1czudfh Commented Nov 11, 2020 at 16:57
  • the Task component has different prop Commented Nov 11, 2020 at 17:02
  • 1
    Try adding this below v-for statement.................... v-show="example.exampleID === 3" Commented Nov 11, 2020 at 17:20
  • 1
    @DavidD if the below answers are working fine please do upvote it so that it would be encouraging. Thanks Commented Nov 11, 2020 at 17:29

2 Answers 2

2

You shouldn't use v-if and v-for in the same element, in this case i suggest to use a computed property that only return the desired example :

data () {
return{
  examples: [
    {
      exampleID: 5,
      exampleText: 'foo'
    },
    {
      exampleID: 3,
      exampleText: 'bar'
    }
  ]
}
},
computed:{
    myExample(){
       return this.examples.find(ex=>ex.exampleID===3)
    }
}

then render it like :

  <Task   :example="myExample"/>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this one works just as fine as the v-show directive, but I am still gonna stick to your solution if you say it's more performant!
2

Another efficient way of doing it without v-for is

data () {
return{
  examples: [
    {
      exampleID: 5,
      exampleText: 'foo'
    },
    {
      exampleID: 3,
      exampleText: 'bar'
    }
  ],
  id: 3
}
},
computed:{
    myExample(){
       return id => this.examples.find(ex=>ex.exampleID===id)
    }
}

rendering part will be like

<Task :example="myExample(id)"/>

In this way you no need to hardcode the value as 3 in the computed property.

2 Comments

computed is a property not a method, it doesn't accept parameters, try to ,call it id with this something like return this.examples.find(ex=>ex.exampleID===this.id) and <Task :example="myExample"/>
could you provide running example? you could use the running example in this answer

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.