Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
Ramda.js: How to pass same argument when we compose

I have to following code:

R.map(
  object => ({
    ...object,
    sth: R.compose(
     R.filter(R.propEq('key', val)),
     R.prop('sth')
    )(object)
  }),
  array
)

Is there a way to write it cleaner, without passing literal function to map. We want to R.assoc the result of compose to object. But we need to pass the same arg to assoc as 3rd arg, because we get the correct value, but we need to use same object as source to make a copy from in assoc:

R.map(
  R.compose(
    R.assoc('sth'),
    R.filter(R.propEq('key', val)),
    R.prop('sth')
  ),
  array
)
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use R.evolve to transform some of an object's properties without effecting the others:

const { map, evolve, filter, propEq } = R

const matchSth = val => map(evolve({
  sth: filter(propEq('key', val))
}))

const arr = [{"foo":1,"sth":[{"key":2,"bar":"a"},{"key":1,"bar":"b"},{"key":2,"bar":"c"}],"baz":3},{"foo":2,"sth":[{"key":1,"bar":"c"},{"key":1,"bar":"d"},{"key":2,"bar":"e"}],"baz":5}]

console.log(matchSth(2)(arr))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Without some sample data, I'm mostly making a guess here. But I think this might do what you want:

const matchSth = (val) => map (
  over (lensProp ('sth')) (filter (propEq ('key') (val)))
)

const arr = [
  {foo: 1, sth: [{key: 2, bar: 'a'}, {key: 1, bar: 'b'}, {key: 2, bar: 'c'}, ], baz: 3},
  {foo: 2, sth: [{key: 1, bar: 'c'}, {key: 1, bar: 'd'}, {key: 2, bar: 'e'}, ], baz: 5}
]

console .log (matchSth (2) (arr))
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script>const {map, over, lensProp, filter, propEq} = R                 </script>

Lenses are very useful abstractions, and here lensProp ('sth') focuses our attention on the sth property of its target. over clones an object, except that it applies the function supplied to the value at that focus. Here we pass a function which filters those whose key properties match the supplied value.

While we could certainly find a way to make this point-free, I find this readable and understandable as it is, and probably wouldn't bother pursuing it.

about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda