【问题标题】:Create array of strings by replacing multiple words in a start string通过替换起始字符串中的多个单词来创建字符串数组
【发布时间】:2023-02-22 18:39:18
【问题描述】:

我正在尝试从传递到我的函数的单个字符串创建一个句子数组。 “起始字符串”包含可以用存储在我的函数之外的列表中的任何项目替换的单词或短语。例如,给定

var swaps = [
  {name: "animal", replacements: ["cat", "dog", "rabbit"]},
  {name: "location", replacements: ["the park", "bed with a cold", "the back seat of the car"]},
  {name: "man", replacements: ["Pete", "Tom", "Robert"]}
]

var sentence = "I saw |man| in |location| with his pet |animal|."

我想创建一个将输出的函数

[
  "I saw Pete in the park with his pet cat.",
  "I saw Tom in the park with his pet cat.",
  "I saw Robert in the park with his pet cat.",
  "I saw Pete in the park with his pet dog.",
  "I saw Tom in the park with his pet dog.",
  "I saw Robert in the park with his pet dog.",
  "I saw Pete in the park with his pet rabbit.",
  "I saw Tom in the park with his pet rabbit.",
  "I saw Robert in the park with his pet rabbit.",
  "I saw Pete in bed with a cold with his pet cat.",
  "I saw Tom in bed with a cold with his pet cat.",
  "I saw Robert in bed with a cold with his pet cat.",
  "I saw Pete in bed with a cold with his pet dog.",
  "I saw Tom in bed with a cold with his pet dog.",
  "I saw Robert in bed with a cold with his pet dog.",
  "I saw Pete in bed with a cold with his pet rabbit.",
  "I saw Tom in bed with a cold with his pet rabbit.",
  "I saw Robert in bed with a cold with his pet rabbit.",
  "I saw Pete in the back seat of the car with his pet cat.",
  "I saw Tom in the back seat of the car with his pet cat.",
  "I saw Robert in the back seat of the car with his pet cat.",
  "I saw Pete in the back seat of the car with his pet dog.",
  "I saw Tom in the back seat of the car with his pet dog.",
  "I saw Robert in the back seat of the car with his pet dog.",
  "I saw Pete in the back seat of the car with his pet rabbit.",
  "I saw Tom in the back seat of the car with his pet rabbit.",
  "I saw Robert in the back seat of the car with his pet rabbit."
]

可以有任意数量的替换,不一定是 3 个。我想它需要是一个调用自身的函数,但我不知道从哪里开始!

我已经尝试在竖线字符 (|) 处拆分字符串并且我可以成功替换第一个短语,但是当我不知道还有多少短语可能需要替换时,我不知道如何处理结果数组。

非常感谢您的指点。

【问题讨论】:

    标签: javascript string loops replace iteration


    【解决方案1】:
    function generateSentences(template, swaps) {
      let result = [template];
      swaps.forEach(swap => {
        let newResult = [];
        result.forEach(r => {
          swap.replacements.forEach(rep => {
            let newStr = r.replace(`|${swap.name}|`, rep);
            newResult.push(newStr);
          });
        });
        result = newResult;
      });
      return result;
    }
    
    
    const swaps = [
      { name: "animal", replacements: ["cat", "dog", "rabbit"] },
      { name: "location", replacements: ["the park", "bed with a cold", "the back seat of the car"] },
      { name: "man", replacements: ["Pete", "Tom", "Robert"] }
    ];
    
    const sentence = "I saw |man| in |location| with his pet |animal|.";
    
    const sentences = generateSentences(sentence, swaps);
    
    console.log(sentences);
    

    【讨论】:

      【解决方案2】:

      也许这就是你想要的。

      function generateSentences(swaps, sentence) {
        let sentences = [sentence];
       
        swaps.forEach((swap) => {
          let newSentences = [];
       
          swap.replacements.forEach((replacement) => {
            sentences.forEach((prevSentence) => {
              newSentences.push(prevSentence.replace(`|${swap.name}|`, replacement));
            });
          });
       
          sentences = newSentences;
        });
       
        return sentences;
      }
       
      var swaps = [
          {name: "animal", replacements: ["cat", "dog", "rabbit"]},
          {name: "location", replacements: ["the park", "bed with a cold", "the back seat of the car"]},
          {name: "man", replacements: ["Pete", "Tom", "Robert"]}
        ]
       
        var sentence = "I saw |man| in |location| with his pet |animal|."
       
        var res = generateSentences(swaps,sentence);
        console.log(res);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 2022-01-17
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多