繁体   English   中英

追加到字符串Ramda.js的末尾

[英]Append to end of string Ramda.js

我试图使用Ramda将字符串附加到字符串的末尾,但以下不起作用。

R.append("A", "B")

它返回

['A','B']

有没有人知道在Ramda中这样做的好方法,或者这并不意味着我需要编写代码来讨论javascripts concat函数?

编辑:

我正在尝试执行以下操作

 props = { city: "Boston", state: "Massachusetts", zip: 22191 } var appendCommaToCity = R.evolve({city: R.append(",")} appendCommaToCity(props) 

除非我在其他地方定义函数并对其进行咖喱,或者将其定义为内联,否则执行"A" + "B"在此剪辑中不起作用。

正如其他人所指出的那样, concat是最简单的方法,使用以下任一技术:

const appendCommaToCity = R.evolve({city: R.concat(R.__, ",")})
// or 
const appendCommaToCity = R.evolve({city: R.flip(R.concat)(",")})

第一个,使用占位符可能更容易。

但我猜你的结果是一个中间结构,后来将用于将该城市加入该州。 如果是这种情况,那么这可能是过度的。 至少在现代JS中,将它们直接组合起来简直太容易了:

const foo = ({city, state}) => `${city}, ${state}`

您可以在Ramda REPL中看到这一切。

console.log(R.concat("ACD", "BEF")); // concatenates strings as they are
// => ACDBEF
// Or
var arr = R.append("ACD", "BEF"); // creates array ["B","E","F","ACD"]
var last = [arr.pop()]; // gets the last element "ACD" and removes it from the array
console.log(last.concat(arr).join('')); // adds the last to the beginning of the array then joins all to form a string
// => ACDBEF
// Or
console.log("ACD" + "BEF"); // simple js string addition
// => ACDBEF
// ++ There are more js methods

 console.log("A".concat("B").concat("C")) console.log("".concat("A", "B", "C")) console.log(String.prototype.concat("A", "B", "C")) 

但只是"A" + "B" concat快很多

暂无
暂无

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

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