var data = {
type: 'TEST',
active: true,
letters: Immutable.Map({ 'a': true, 'b': false })
};
var dataToLog = _.object(
_.map(data, function(v, k) {
if (Immutable.Iterable.isIterable(v)) {
return ['*Immutable* - ' + k, v.toJS()];
} else {
return [k, v];
}
})
);
console.log('Output: ', dataToLog);
"Output: "
[object Object] {
*Immutable* - letters: [object Object] {
a: true,
b: false
},
active: true,
type: "TEST"
}
I have this in a JSBin here.
Using lodash and some of the transform methods to manipulate Immutable objects flowing through my Flux Dispatcher to JSON for easier debugging and also indicate by prepending 'Immutable' to the key's name in the output. It works well, but I'm learning about using _.map, _.compose, _.curry, and wonder if this could be made even more functional.
Specifically, I'm wondering how to handle this if/else that's in my function sent to _.map:
if (Immutable.Iterable.isIterable(v)) {
return ['*Immutable* - ' + k, v.toJS()];
} else {
return [k, v];
}
I'm not understanding how I can transform this into something functional other than making the check for Immutable and return of that into separate functions that check for Immutable values of the object and another to transform it to JSON (making the call to Immutable's .toJS()).
I'm also struggling with how to operate on this and get the desired output while operating on this as an object instead of having my map function return an array of the key and value.
To clarify, the goals for the overall transformation:
- Iterate over an object with key, value
- If value is NOT an Immutable object, return key and value unaltered
- If value IS an Immutable object, modify key to be prefixed with 'Immutable' and call .toJS() on value and return those transformations
if ... elsewhere theifclause always ends withreturnis redundant. You just need anifand noelse.)ifis not functional?transformmethod._.transform(data, function(result, v, k) { if(Immutable.Iterable.isIterable(v)) return result['*Immutable* - ' + k] = v.toJS(); else return result[k] = v; });