Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting index of a data in an array in VUE js

I want to change the status of Tasks when a particular method is called. But The problem is I cannot get the index of the particular item of the array to change its status. This is my HTML:

<div class="main" id="my-vue-app">
    <ul>
        <li v-for="task in completeTask">
            {{ task.description }} <button @click="markIncomplete">Mark as Incomplete</button>
        </li>

    </ul>
    <ul>
        <li v-for="task in incompleteTask">
            {{ task.description }} <button @click="markComplete">Mark as Complete</button>

        </li>
    </ul>
</div>

And this is my Vue:

<script>
    new Vue(
        {
            el: '#my-vue-app',
            data:
            {
                tasks: [
                {description:'go to market', status: true},
                {description:'buy book', status: true}, 
                {description:'eat biriani', status: true}, 
                {description:'walk half kilo', status: false}, 
                {description:'eat icecream', status: false}, 
                {description:'return to home', status: false}
                ]
            },
            computed: 
            {
                incompleteTask()
                {
                    return this.tasks.filter(task => ! task.status);
                },
                completeTask()
                {
                    return this.tasks.filter(task => task.status);
                }
            },
            methods: 
            {
                markComplete()
                {
                    return this.task.status = true;

                },
                markIncomplete()
                {
                    return this.task.status = false;
                }
            }
        }
    )
</script>

I need make use of markComplete() and markIncomplete() but the problem is I couldn't find the way to get the index of current element to change its status.

like image 707
Delowar Hossain Avatar asked Apr 06 '18 16:04

Delowar Hossain


People also ask

How do I extract an index from an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.

How do you find the index of a value in an array in JS?

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

How do you access the elements of an array using an index?

Access Array Elements Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

Can you use index for array?

It is not possible to define an array section using Indexes, but one can use them in gather/scatter-like operations. The only restriction is that elements in an Index must be nonnegative. The same value can appear multiple times in an Index.


1 Answers

You could get the index by declaring a second argument at the v-for:

<li v-for="(task, index) in incompleteTask">
    {{ task.description }} <button @click="markComplete(index)">Mark as Complete</button>
</li>
    methods: 
    {
        markComplete(index)
        {
            return this.tasks[index].status = true;

        },

But a, maybe simpler, alternative is to simply **pass the `task` as argument**:
<li v-for="task in incompleteTask">
    {{ task.description }} <button @click="markComplete(task)">Mark as Complete</button>
</li>
    methods: 
    {
        markComplete(task)
        {
            return task.status = true;

        },
like image 182
acdcjunior Avatar answered Oct 22 '22 12:10

acdcjunior