• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

104
Vistas
_.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?

almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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>

almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda