Ramda减少字符串以替换之前的字符串

时间:2017-03-08 00:26:03

标签: javascript ramda.js

我有这个字符串列表,我想用ramda减少:

["red blue green", "pink blue green", "red orange green", "red blue black"]

我想减少列表,以便我留下:

"pink orange black"

所以基本上只替换前一个字符串的差异,而不覆盖整个字符串。

步骤应该是:

"red blue green"
"pink blue green"
"pink orange green"
"pink orange black"

这就是我目前所拥有的:

const strings = ["red blue green", "pink blue green", "red orange green", "red blue black"]

const replaceString = (old, acc) => R.replace(old, acc, acc)

R.reduce(replaceString, R.head(strings))

1 个答案:

答案 0 :(得分:2)

我的代码可以从初始数据中获得您想要的答案。但我真的不知道它是否能解决你的一般问题:

const colors = ["red blue green", "pink blue green", "red orange green", "red blue black"];

const fn = pipe(
  map(split(' ')),    // [['red', 'blue', 'green'], ['pink', 'blue' 'green'], 
                      //   ['red', 'orange', 'green'], ['red' 'bluee', 'black']]
  flatten,            // ['red', 'blue', 'green', 'pink', 'blue' 'green', 
                      //   'red', 'orange', 'green', 'red' 'bluee', 'black']
  countBy(identity),  // {red: 3, blue: 3, green: 3, pink: 1, orange: 1, black: 1}
  filter(equals(1)),  // {pink: 1, orange: 1, black: 1}
  keys,               //  ['pink', 'orange', 'black'
  join(' ')           //  'pink orange black'
)

fn(colors); //=> 'pink orange black'

您可以在 Ramda REPL 上看到这一点。