Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ramda `evolve` nested object

Tags:

ramda.js

I have a list similar to this:

var list = [
  {
    stack: [ 
      {
        file: 'abc'
      }
    ]
  },
  {
    stack: [ 
      {
        file: 'abc'
      },
      {
        file: 'abc'
      }      
    ]
  }
];

I want to change every file name with e.g 'def'. How to do that by using ramda ?

I tried things like:

var trans = {
  file: replace('abc', 'def')
};

var f = R.evolve(trans)

var f2 = R.map(f)
R.map(f2, list)

But it doesn't work. I need to include stack field in solution somehow.

like image 675
Kote Avatar asked Mar 10 '26 13:03

Kote


1 Answers

Well, it's not pretty, but I think this will do it:

R.map(R.over(
  R.lensProp('stack'), 
  R.map(R.over(R.lensProp('file'), R.replace('abc', 'def')))
))(list)

You could probably also use an evolve inside, but lenses are pretty powerful, and more generally useful.

like image 63
Scott Sauyet Avatar answered Mar 12 '26 21:03

Scott Sauyet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!