this.$store.commit( 'deleteCheckboxItems', response.data.items.forEach(element => { element.id; }) );
From the received api
I need to get the id and pass it to vuex. The function works but writes undefined to the console when called.
vuex:
deleteCheckboxItems(state, payload) { if(state.filteredBrands) { state.filteredBrands = state.filteredBrands.filter((item) => { console.log(payload); item.id == payload; }); }
In vuex I need id
to compare, and delete them if they are the same.
What should I change?
If you don't want to change the implementation of
deleteCheckboxItems
, wrapthis.$store.commit
with a for every using an array, like this:response.data.items.forEach(element => { this.$store.commit('deleteCheckboxItems', element) });
Also, and more importantly, your
filteredBrands.filter
will not filter because you have no return value. You mustreturn item.id === Payload
.Use the
map
function instead offorEach
:Your
filter
function also needs some changes. It should return a boolean value, but you forgot to return that value.The payload may be an id array, you should use some functions like
includes
to check your case.