If you are looking to access data by id, using an array may not be the best choice. Consider using an object instead, like users: {}
. Arrays work best when there are minimal gaps between ids.
To ensure reactivity in your values, utilize Vue.set
, as outlined in the Vue.js documentation here: https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats. For instance:
Vue.set(users, user.id, user)
If you opt to continue using an array, there are alternative methods like https://v2.vuejs.org/v2/guide/list.html#Caveats, such as using splice
.
For further information on normalizing data in Vuex, check out this thread: https://forum.vuejs.org/t/vuex-best-practices-for-complex-objects/10143
Update:
Basing on your requirement for key-based lookup and filtering, consider storing values in an object and creating a getter or computed property for Object.values(users)
when needing an array format. Getters and computed properties are cached, minimizing overhead when users change. This array can then be filtered using filter
.