Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
How does _.forEach work in lodash/fp as compared to lodash?

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?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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>

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda