How to replace question marks inside a string with the values of an array?

前端 未结 5 1188
别那么骄傲
别那么骄傲 2020-12-03 19:11

Given the string \'Hello ?, welcome to ?\' and the array [\'foo\', \'bar\'], how do I get the string \'Hello foo, welcome to bar\' in

5条回答
  •  离开以前
    2020-12-03 19:40

    Let's build a replaceWith function:

    const replaceWith =
      (...str) =>
        (fallback) =>
          str.length === 0
            ? fallback
            : str.shift();
    

    It's a curried function: it takes a list of replacement strings and returns another function which will return the replacements one by one:

    const replacements = replaceWith('

提交回复
热议问题