VUE: With recovering an array variable filtering by value

Asked

Viewed 86 times

0

Good afternoon, I need to recover the variable name of this object array, when I filter by id, when I type for example the id 1, return "Solid"

full_category_list: [
    {
        id: 1, 
        name: 'Sólido',
        parent : 0,
    },
    {
        id: 2, 
        name: 'Líquido',
        parent : 0
    }
]

1 answer

3


You can create a computed Property which will calculate which category will be selected.

new Vue({
    el: '#app',
    data: {
        selected: null,
        full_category_list: [
            {
                id: 1, 
                name: 'Sólido',
                parent : 0
            },
            {
                id: 2, 
                name: 'Líquido',
                parent : 0
            }
        ]
    },
    computed: {
        selected_category: function() {
            var category = this.full_category_list.find(cat => cat.id == this.selected);
            return category ? category.name : "Nenhum selecionado";
        }
    }
});
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
  <input v-model="selected">
  <div>{{ selected_category }}</div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.