Mikael Ken
Mikael Ken

Reputation: 41

Ramda path for array values

I have the Ramda code below which won't work. Is it because of appSessions[0]? and if yes, how should I write? Also how do I add a default if that value isn't found?

R.path(['appSessions[0]', 'personalInfo', 'personalInfo'], response);

Upvotes: 2

Views: 6314

Answers (1)

Scott Sauyet
Scott Sauyet

Reputation: 50807

You don't want ['appSession[0]', ...] but ['appSession', 0, ...]:

The nodes supplied to path can be either strings (for objects) or numbers (for arrays):

const response = {
  appSessions: [
    {
      id: 1,
      personalInfo: { personalInfo: {foo: 'bar'}, other: true },
      another: 1
    }, {
      id: 2,
      personalInfo: { personalInfo: {foo: 'qux'}, other: 99 },
      another: false
    }
  ]
}

console .log (
  R .path (['appSessions', 0, 'personalInfo', 'personalInfo'], response)
)
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

That actual path looks strange to me, with personalInfo nested inside personalInfo, but it will work.

Upvotes: 8

Related Questions