In my Vuex store i have a action which once get result, loop over each element of the result and add's new property to each one.
Vue.axios
.get(endpoint, headers)
.then(res => {
if (Array.isArray(res.data) && res.data.length) {
res.data.forEach(el => {
el.link = 'some_dynamic_link'
})
commit('SET_ACTIVITY', res.data)
}
})
the problem is that my store ends up with the data from rsult, WITHOUT the link added to any of the elements.
i thought that something like this tweak will help:
if (_.has(res.data[res.data.length - 1], 'link')) { commit('SET_ACTIVITY', res.data) }
but then the mutation just ends up not executing at all, any idea how can i achieve this synchronous behavior ?
update tried to use map instead:
.then(res => {
if (Array.isArray(res.data) && res.data.length) {
commit('SET_ACTIVITY', res.data.map(el => {
el.link = 'some_dynamic_link'
}))
}
})
but than the store contains activities array where elements are undefined's
mapinstead offorEach. Check this answermapto a variable and passing it to thecommitends up with the store holding array ofundefinedreturn elin yourmap. Each iteration ofmapMUSTreturnsomething that will go into final array