This question may also be applicable to Vue in general, but I am using Vuex for my project.
I have an array of objects in my store for which I would like to have a calculated property.
To simplify, assume my store is as follows:
const state = {
numbers = [
{num: 2, multiplier: 3},
{num: 5, multiplier: 10},
{num: 1, multiplier: 6}
]
};
Now, I want a calculated property on each object of the numbers array, such that the result is num * multiplier (eg. 2*3 = 6, 5*10 = 50)
One solution is to make a calculated property that returns the numbers array, plus the calculated field... eg:
const getterrs = {
num_list(state){
const list = state.numbers
list.map(n=>{
n.value=n.num*n.multiplier;
);
return list;
}
}
That works, but it has a couple issues:
The array returned cannot be bound with v-model on the non-computed fields
The entire array will be recalculated whenever any element in the array is changed... I only want to recalculate the individual element that changed.
Is that possible with Vue/Vuex?