This snippet, which uses lodash's _.forEach
,
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
console.log(key + '=' + value);
})
prints
a=1
b=2
How does _.forEach
work in lodash/fp
?
If I try the snippet above, at the console it results in
ƒ (value, key) {
console.log(key + '=' + value);
}
which is close to "I'm not using it the way I should".
If I try inverting the two inputs (as that's actually one main difference between lodash
and lodash/fp
),
_.forEach(function(value, key) {
console.log(key + '=' + value);
}, { 'a': 1, 'b': 2 })
I get
undefined=1
undefined=2
So my question is: is there a way to _.forEach
on an object via lodash/fp
and have access to both keys and values of the object?
The parameters of the iteratee function are capped in Lodash FP
Quoting the documentation:
Capped Iteratee Arguments
Iteratee arguments are capped to avoid gotchas with variadic iteratees.
// The `lodash/map` iteratee receives three arguments: // (value, index|key, collection) _.map(['6', '8', '10'], parseInt); // ➜ [6, NaN, 2] // The `lodash/fp/map` iteratee is capped at one argument: // (value) fp.map(parseInt)(['6', '8', '10']); // ➜ [6, 8, 10]
Different functions in Lodash FP will have the iteratee is capped to only 1 parameter - _.forEach()
is among them. The only three that have the iteratee capped to two are _.reduce()
, _.reduceRight()
, and, _.transform()
.
In order to iterate over both keys and values in Lodash FP, you can use _.forEach()
over _.entries()
. Since that gives the entries as key-value pairs, you can destructure them:
const obj = { 'a': 1, 'b': 2 };
_.forEach(function([value, key]) { // destructure the pair
console.log(key + '=' + value);
}, _.entries(obj)) // get as pairs
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
A more readable way would be to compose them with _.flow()
const obj = { 'a': 1, 'b': 2 };
const process = _.flow(
_.entries,
_.forEach(function([value, key]) {
console.log(key + '=' + value);
})
);
process(obj);
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
Finally, you can avoid the destructuring, if you wish, by using _.spread()
:
const obj = { 'a': 1, 'b': 2 };
const process = _.flow(
_.entries,
_.forEach(_.spread(function(value, key) {
console.log(key + '=' + value);
}))
);
process(obj);
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>