Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ramda Js: Setting property on an object using a value from the same object

Using Ramda Js, I need to create a function that can set one object property using the value of a different property on the same object. My attempt so far is as follows:

var foo = R.set(R.lensProp('bar'), 'foo' + R.prop('foo'));
var result = foo({foo:"bar"});

Desired result:

{foo:"bar", bar:"foobar"}

Actual result:

{foo:"bar", bar: "foofunction f1(a) {... etc"}

Clearly I'm misunderstanding something here, and any insights into how to approach this would be appreciated.

like image 668
James Flight Avatar asked Nov 28 '22 23:11

James Flight


1 Answers

Lenses are not a good fit when the value of one property depends on the value of another property. A lambda is probably best here:

const foo = o => R.assoc('bar', 'foo' + o.foo, o);

foo({foo: 'bar'});
// => {foo: 'bar', bar: 'foobar'}
like image 167
davidchambers Avatar answered Dec 01 '22 13:12

davidchambers