Reputation: 331
I have a somewhat complex state as follows:
array:
array:
object,
object,
etc...
array:
etc...
Simply put, an array containing a number of arrays that hold objects. I am trying to find one of these objects inside this mess according to its Id and update it (namely setting a completed value to true), but haven't been able to so far. Using Mudash, I managed to find this object, but I have no idea how to pass it up again to return it, my code is as follows:
return state.update('data', (arr) => {
_.forEach(arr, function(item) {
_.forEach(item, function(elt) {
if(elt.Id == action.Id) {
console.log('Item Found');
elt.set('completed', true);
}
});
});
return arr;
});
Is there some simple way to accomplish this? Thank you.
Upvotes: 1
Views: 435
Reputation: 913
The key here is that you cannot mutate values in place. Instead, you have you approach the problem from thinking about how you change every parent value in the deeply nested data structure.
A simple use of the _.map method will do the trick...
const _ = require('mudash')
return _.update(state, 'data', (arr) => {
return _.map(arr, function(item) {
return _.map(item, function(elt) {
if(_.get(elt, 'Id') == action.Id) {
console.log('Item Found');
return _.set(elt, 'completed', true);
}
return elt;
});
});
});
Upvotes: 1
Reputation: 111336
If you are asking how to mutate a part of an immutable object then there will be no answer that is going to satisfy you. See this answer for much more details:
Upvotes: 1