简体   繁体   English

如何使用Ramda函数在索引处接受参数?

[英]How can I take an argument at an index with Ramda functions?

Note 注意

Firstly, I would like to post this to the code review site; 首先,我想将其发布到代码审查站点; but you need at least 3 lines of code for that, and this question doesn't necessitate that much code. 但是您至少需要三行代码,而这个问题并不需要那么多的代码。 I'm aware this question is a little vague for SO, but I can't see a preferable site on which to post it. 我知道这个问题对于SO来说有点模糊,但是我看不到在其上发布它的更好的网站。


The actual question 实际问题

I have found it useful in a couple of situations, particularly when using ramda, to be able to have a function which simply returns argument n which was passed to the function. 我发现它在几种情况下很有用,尤其是在使用ramda的情况下,它能够具有一个仅返回传递给该函数的参数n的函数。

The easiest way I've been able to produce that is: 我能够产生的最简单方法是:

const takeArgument = argIndex => (...args) => args[argIndex];

This is fine, and works for what I need, but my question is, is there a way to do this using only Ramda functions? 很好,并且可以满足我的需要,但是我的问题是,有没有办法仅使用Ramda函数来做到这一点? I've not been able to find any examples of this being done, but I feel like there must be a simple way to do something like this. 我还没有找到完成此操作的任何示例,但我觉得必须有一种简单的方法来执行此操作。

As @Thomas mentioned in his comment, this is easily achieved with nthArg , for instance: 正如@Thomas在他的评论中提到的,例如,可以使用nthArg轻松实现:

 const { nthArg } = R; const getSecondArg = nthArg(1) const x = getSecondArg(1, 2) console.dir(x) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM