admin管理员组

文章数量:1201353

I am trying to return the largest element in a list page:

page = [1,2,3];
R.max(page); // returns a function.
R.max(-Infinity, page); // seems correct but doesn't work as expected.

I am trying to return the largest element in a list page:

page = [1,2,3];
R.max(page); // returns a function.
R.max(-Infinity, page); // seems correct but doesn't work as expected.
Share Improve this question edited Mar 17, 2017 at 9:05 Angelos Chalaris 6,7478 gold badges53 silver badges79 bronze badges asked Jul 19, 2015 at 18:13 JimJim 16k16 gold badges89 silver badges174 bronze badges 3
  • 1 as Javascript is tagged, Math.min.apply(this,[1,2,3]) – vinayakj Commented Jul 19, 2015 at 18:15
  • Note that your title contradicts your question -- do you want the smallest or largest item? I answered for the largest item, getting the smallest one should be easy if you understand the mechanics. – Frédéric Hamidi Commented Jul 19, 2015 at 18:25
  • 1 Yes, as of 0.16 Ramda's max and min functions are binary, for reasons described in Issue 1230 and implemented in PR 1231. The answer from @FrédéricHamidi is probably the simplest way to do this. Obviously you could create a stand-alone function for this if you need it often. – Scott Sauyet Commented Jul 19, 2015 at 21:59
Add a comment  | 

2 Answers 2

Reset to default 23

I don't have the ramda package installed, so this is untested, but from the documentation max() only takes two arguments, so you would have to reduce() your array upon it:

var page = [1, 2, 3],
    result = R.reduce(R.max, -Infinity, page);
// 'result' should be 3.

Use the apply function: https://ramdajs.com/0.22.1/docs/#apply

const page = [1, 2, 3];
R.apply(Math.max, page); //=> 3

本文标签: javascriptObtaining the largest item in a list using ramdaStack Overflow