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 it writes undefined in 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, if there are the same then delete.
What should I change?
CodePudding user response:
Use map
function instead of forEach
:
this.$store.commit(
'deleteCheckboxItems',
response.data.items.map(({id}) => id)
);
CodePudding user response:
if you don't want to change your implementation of deleteCheckboxItems
to take an array wrap the this.$store.commit
with the for each like this:
response.data.items.forEach(element => { this.$store.commit('deleteCheckboxItems', element) });
Furthermore, the more important one is that your filteredBrands.filter
does not filter since you do not return a value. You have to return item.id === payload
.