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

126
Visualizações
_.filter function does not work with _.partialRight constructed function

The _.filter function does not work with _.partialRight constructed function, but works with a custom function. See below example:

import _ from 'lodash';

const students = [
  {firstName: "Mary"},
  {firstName: "Jane"},
  {firstName: "John"},
];

function firstNameStartsWith(student, letter) {
  return _.startsWith(student.firstName, letter);
}

_.filter(students, _.partialRight(firstNameStartsWith, "J"));
// []

_.filter(students, student => firstNameStartsWith(student, "J"));
// [ { firstName: 'Jane' }, { firstName: 'John' } ]

Can someone explain why?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The _.filter() function passes 3 parameters to the predicate - value, index, and the original array. The index overrides the partially applied value, and you get the wrong result. This happens because "partially applied arguments are appended to the arguments it receives". You can fix this by limiting the partially applied function to a single parameter using _.unary():

const students = [
  {firstName: "Mary"},
  {firstName: "Jane"},
  {firstName: "John"},
];

function firstNameStartsWith(student, letter) {
  return _.startsWith(student.firstName, letter);
}

const result = _.filter(students, _.unary(_.partialRight(firstNameStartsWith, "J")));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza 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