10

I would like to avoid image value in below code.image is a key for property. How can I do that ?

<tbody>
    <tr v-for="obj in data" :id="obj.id">
       <td v-for="property in obj">{{property}}</td>
    </tr>
</tbody>
6
  • 1
    I'm not really sure what you're asking. What are you trying to do? Not make a td for any object that has an image? Commented Dec 18, 2017 at 2:53
  • 1
    Thanks @HEATH3N for your reply. There are different values in {{property}}. But I would not like to print value of image property. Commented Dec 18, 2017 at 4:06
  • 1
    So is property an object? Because the way you're calling property.image suggests that it is. Commented Dec 18, 2017 at 4:17
  • No, property is a value. I am updating my question. I don't know how to do that's why I was calling property.image. Thanks Commented Dec 18, 2017 at 4:18
  • 1
    See vuejs.org/style-guide/… Commented Jul 14, 2023 at 19:26

2 Answers 2

17

The Accepted answer is an anti-pattern because you should not mix v-for and v-if on the same node in VueJs 2+ as Thomas van Broekhoven pointed out. Instead, you can just chain a filter onto the object. Here is an example using an ES6 arrow function which should* work.

  • Untested syntax. Typing on my phone, in bed.
<tbody>
    <tr v-for="obj in data" :id="obj.id">
       <td v-for="property in obj.filter(property => property !== 'image')">{{property}}</td>
    </tr>
</tbody>
Sign up to request clarification or add additional context in comments.

1 Comment

eslint-plugin-vue errors tho, says 'v-for' directives require that attribute value.
5

Let check it out: v-for with an Object, v-for with v-if.

<td v-for="(value, property) in obj" v-if="property!='image'">
    {{value}}
</td>

6 Comments

Thanks @Ben for your reply. I tried like this <td v-for="property in obj" v-if="!property.image">{{property}}</td>. But it is not working. Thanks
I guess you should try <td v-for="(value, property) in obj" v-if="property!='image'">{{value}}</td>
^ That works and is much better than my mess of an answer.
Note that it’s not recommended to use v-if and v-for together. Refer to style guide for details. (vuejs.org/v2/guide/list.html#v-for-with-v-if)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.