3

I am just starting using Vuex but am having a problem (and this could be some clueless syntax error on my part). I have a user with liked_items and have a network call that is to unlike an item.

mutations: {
SET_TOGGLE_LIKED: (state, { global_id }) => {
  alert('here is global_id: ' + global_id)
 state.user.liked_items.find((obj,i) => {
   if(obj.global_id === global_id){ // no global_id
      console.log('that exists at i: ' + i + ' with length: ' + state.user.liked_items.length)
      state.user.liked_items.splice(i, 1)
      console.log('that exists at i: ' + state.user.liked_items.length)
   }else{
     console.log('that doesnot exist!')
   }
 })
}

The problem I'm having is that after removing an item from the liked_items list, that seems to get recalled and I get an error that global_id does not exist on undefined.

I am able to fix by having:

 state.user.liked_items.find((obj,i) => {
   if(obj){
     if(obj.global_id === global_id){

but why do I need to check for the existence of obj here?

3 Answers 3

16

We can also use map to get the index of the object in a store array and remove it using:

Mutation

REMOVE_OBJECT_FROM_ARRAY: (state, payload) => {
    const i = state.someArrayofObjects.map(item => item.id).indexOf(payload.id);
    state.someArrayofObjects.splice(i, 1);
  }

Here, the id is the id passed with the payload to the MUTATION, we can also pass only the id as the whole payload. In that case, we can do something like:

REMOVE_OBJECT_FROM_ARRAY: (state, payload) => {
    const i = state.someArrayofObjects.map(item => item.id).indexOf(payload);
    state.someArrayofObjects.splice(i, 1);
  }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but why not use findIndex(item => item.id === payload)?
Thank you very much. why doesn't this work with the filter?
Thank you for this answer, we should add a condition to check if the index exists to avoid deleting all items like this if (i != -1) { state.someArrayofObjects.splice(removeIndex, 1); }
This doesn't answer the question: why do I need to check for the existence of obj here?
you are looking for the index of the object in order to delete it and you are not check for existence of the object.
2

If you have undefined as elements of the liked_items array, you'll get obj as undefined sometimes.

You could simplify your if as below, though:

 state.user.liked_items.find((obj,i) => {
   if(obj && obj.global_id === global_id){

Or could pre-filter the array to keep only non-undefined values:

 state.user.liked_items.filter(o => o).find((obj,i) => {
   if(obj.global_id === global_id){

Comments

-2

You can use the following mutation, it uses the splice from Javascript array methods.

deleteElement: function(state.element) {
  state.myArray.splice(this.events.indexOf(element), 1);
}

If you know the index of the element, it becomes (obviously):

deleteElement: function(state,elementIndex) {
  state.myArray.splice(elementIndex, 1);
}

"1" is the number of elements to remove, starting from elementIndex

1 Comment

The question is why do I need to check for the existence of obj here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.