VueJs: How to Edit an Array Item
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Array Updates in Vue.js: The Best Way to Edit Array Items
As developers working with reactive frameworks like Vue.js, managing data structures—especially arrays of objects—is a fundamental task. When we move beyond simple display and enter the realm of editing, we run into challenges regarding immutability and reactivity. Many developers attempt direct DOM manipulation or manual array indexing, which often leads to state corruption and broken views.
This post addresses the common dilemma: How do you effectively edit a specific item within an array in Vue without sacrificing data integrity?
The Pitfalls of Direct Mutation in Vue
The initial approach you described—using indexOf to find an item and directly modifying properties on that object within the array—is a valid starting point, but it often fails to trigger Vue’s reactivity system correctly if not handled carefully. When dealing with nested data structures, especially when using directives like v-repeat, Vue relies on detecting changes in the state object itself to update the DOM efficiently. Directly manipulating array indices or properties can sometimes bypass these mechanisms, leading to stale data or unexpected UI behavior.
The core principle we must adhere to is ensuring that any change to the state notifies Vue correctly so it can manage the view reliably.
The Recommended Approach: Immutability and Reassignment
The most robust and idiomatic way to handle array updates in Vue is by embracing immutability. Instead of modifying the existing array in place, we create a new array containing the updated item. This signals to Vue that the state has changed, allowing it to re-render the component correctly.
This pattern aligns perfectly with principles found in robust backend systems, such as how data is managed and persisted in frameworks like Laravel, where creating new models or updating relationships involves careful state management rather than destructive updates.
Step-by-Step Implementation
Let's look at how to implement an editTask function using this immutable pattern. We will use the array’s map() method, which is perfect for transforming every item in an array into a new structure.
Consider an array of tasks:
// Initial State Example
data: [
{ id: 1, body: 'Buy groceries', completed: false },
{ id: 2, body: 'Finish Vue tutorial', completed: true }
]
To edit the item at a specific index, we use map() to iterate over the array. When we find the index of the task we want to change, we create a new array using the spread operator (...) and replace the old element with the newly modified object.
Code Example: Immutable Update
Here is how you would structure the method to update an array item immutably:
methods: {
editTask(index) {
// 1. Create a copy of the original array using map()
this.tasks = this.tasks.map((task, i) => {
// Check if this is the task we want to edit (using index or ID)
if (i === index) {
// Return a NEW object with the updated properties
return {
...task, // Copy existing properties
body: document.querySelector('[data-index="' + index + '"]').value, // Get new value from input
completed: false // Example: resetting completion status
};
}
// Return all other tasks unchanged
return task;
});
}
}
Note on Implementation: In a real-world scenario, you would typically bind your input directly to the item being edited using v-model on that specific array element. This allows Vue to handle the change detection automatically. If direct binding is problematic (as you experienced with v-repeat), using methods like splice() combined with explicit state updates inside a carefully scoped function is necessary, ensuring the assignment happens directly on the reactive data source (this.tasks).
Conclusion: Prioritizing Reactivity
The best way to edit an element in a Vue array is not through fragile direct DOM manipulation, but by treating your data as immutable state. By leveraging methods like map() and object spread syntax (...), you ensure that every modification triggers Vue's reactivity system correctly. This approach keeps your application predictable, maintainable, and fully aligned with Vue's core philosophy, making complex data interactions much smoother. Always strive for declarative state changes over imperative DOM manipulation when working within a reactive environment, whether you are building frontends or managing backend structures like those in Laravel.