admin 管理员组

文章数量: 1086019

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);

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);
Share Improve this question edited Aug 30, 2019 at 13:47 custommander 19.1k6 gold badges68 silver badges93 bronze badges asked Aug 28, 2019 at 17:08 Mikael KenMikael Ken 411 silver badge10 bronze badges 4
  • You're looking for pathOr – custommander Commented Aug 28, 2019 at 17:10
  • but what about appSessions[0] ? is that correct ? – Mikael Ken Commented Aug 28, 2019 at 17:10
  • how your response object looks like ? – Code Maniac Commented Aug 28, 2019 at 17:10
  • Not it isn't. It's a path of its own – custommander Commented Aug 28, 2019 at 17:11
Add a ment  | 

1 Answer 1

Reset to default 8

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./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.

本文标签: javascriptRamda path for array valuesStack Overflow