简体   繁体   English

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

[英]Append to end of string Ramda.js

I am trying to append a string to the end of a string using Ramda, but the following does not work. 我试图使用Ramda将字符串附加到字符串的末尾,但以下不起作用。

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

it returns 它返回

['A','B']

Does anybody know a good way to do this in Ramda or in a way that does not mean I need to write code to curry javascripts concat function? 有没有人知道在Ramda中这样做的好方法,或者这并不意味着我需要编写代码来讨论javascripts concat函数?

Edit: 编辑:

I am trying to do the following 我正在尝试执行以下操作

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

Doing "A" + "B" does not work in this sitchuation unless I define the function somewhere else and curry it, or define it inline. 除非我在其他地方定义函数并对其进行咖喱,或者将其定义为内联,否则执行"A" + "B"在此剪辑中不起作用。

As others have pointed out, concat is the simplest approach, using either of these techniques: 正如其他人所指出的那样, concat是最简单的方法,使用以下任一技术:

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

The first one, using the placeholder might be easier here. 第一个,使用占位符可能更容易。

But I'm guessing that your result is an intermediate structure that will later be used to join that city with the state. 但我猜你的结果是一个中间结构,后来将用于将该城市加入该州。 If that's the case, then this might be overkill. 如果是这种情况,那么这可能是过度的。 It's simply too easy at least in modern JS, to combine them directly: 至少在现代JS中,将它们直接组合起来简直太容易了:

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

You can see this all in action in the Ramda REPL . 您可以在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")) 

But just "A" + "B" is a lot faster than concat 但只是"A" + "B" concat快很多

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

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