D
D
Dmitry2021-01-24 12:18:49
Vue.js
Dmitry, 2021-01-24 12:18:49

What is the most efficient way to remove an object from an array in Vuex?

1 option

mutation(state, { id }) {
  state.arr = state.arr.filter(item => item.id != id)
}


Option 2
mutation(state, { id }) {
  Vue.delete(state.arr, state.arr.findIndex(item => item.id == id))
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2021-01-26
@kulakoff Vue.js

Both options are about the same - the speed of searching for an index to delete will be O (N). The most efficient option is to use an object with id keys, then the lookup speed is O(1).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question