用数组中的元素替换字符“ *”的实例-JavaScript

时间:2019-04-22 22:50:59

标签: javascript arrays string function ecmascript-6

我知道我需要一个空白的空数组,如果不是星号,则将句子中的字符推入数组。

我认为这将有助于我理解实际要遍历数组中的项并将其用作替换项后的处理方法。

function replaceAsterisk(sentence, newWords) {

  let newArray = [];

  let character = sentence.split("");

  console.log(character);

  // if the character is not an asterisk, push it into the new array
  if (character !== "*") {
    newArray.push(character);
  }

  // if the character is an asterisk, push "cat" into the new array
  else {
    newArray.push("cat");
  }
  // return new array as a string
  return newArray.join(" ");
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

即使是现在,这也没有将“ cat”推送到数组中-为什么?

6 个答案:

答案 0 :(得分:3)

String.replace()与产生替换字符串的函数一起使用。在函数中,使用计数器从newWords获取当前替换项:

function replaceAsterisk(sentence, newWords) {
  let counter = 0;
  
  return sentence.replace(/\*/g, () => newWords[counter++] || '');
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

答案 1 :(得分:2)

您必须先循环浏览所有字符,请参见示例下方。

function replaceAsterisk(sentence, newWords) {

  let newArray = [];

  let character = sentence.split("");

  console.log(character);
  
  character.forEach(function(c){
    // if the character is not an asterisk, push it into the new array
    if (c !== "*") {
      newArray.push(c);
    }

    // if the character is an asterisk, push "cat" into the new array
    else {
      newArray.push("cat");
    }
  });
  // return new array as a string
  return newArray.join("");
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

答案 2 :(得分:1)

您需要遍历句子中的所有字符:

function replaceAsterisk(sentence, newWords) {
  let newArray = [];

  for( let i = 0; i < sentence.length; i++) { // This line
    character = sentence[i];
    // if the character is not an asterisk, push it into the new array
    if (character !== "*") {
      newArray.push(character);
    }
  
    // if the character is an asterisk, push "cat" into the new array
    else {
      newArray.push("cat");
    }
  }
  // return new array as a string
  return newArray.join(" ");
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));

答案 3 :(得分:1)

这有效。在空字符串上拆分可能很奇怪(请参见MDN...split

  let newArray = [], sentence = "My name is * and I am a *.";      
  for (let char of sentence){ newArray.push(char); }
  for(let i = 0; i < newArray.length; i++){
    if(newArray[i] == "*"){ newArray[i] = "cat"; }
  }
  console.log(newArray);

答案 4 :(得分:1)

您可以使用正则表达式组匹配和destinationGainNode.gain.value = 1;

Array.prototype.shift

答案 5 :(得分:0)

您需要遍历句子中的每个字符,并使用shift来替换该单词:

function replaceAsterisk(sentence, newWords) {
  let newStr = "";
  [...sentence].forEach(char => {
    if (char != "*") {
      newStr += char;
    } else {
      newStr += newWords.shift();
    }
  });
  return newStr;
}

console.log(replaceAsterisk("My name is * and I am a *.", ["Sabrina", "Black Cat", "extra", "words"]));