【问题标题】:Changing the value of an index inside an array更改数组内索引的值
【发布时间】:2021-10-11 01:25:47
【问题描述】:

我遇到了这个问题,我需要从每个数组值中删除一个字母,如果它在索引 0 中,则更改第一个字母。我目前的方法是:

function capital(arr, char){
    let str = "";
    let result = "";
for (let i = 0; i < arr.length; i++){
    str = arr[i] + (i < arr.length - 1 ? ",": "");;

    for (let j = 0; j < str.length; j++){
        if (str[j] === char){
            result += "";
             if (str[j] === char){
            result += (j === 0? "A": "");
        }
        
        else {
            result += str[j];
        }
    }
}
    console.log(result);
}
capital(["doritos","sandking","bandana", "demand"], "d");

如果 d 在索引 0 中,程序应消除字符串中出现的每个字母 d,并将索引 0 更改为字母 A。

当前结果是

Aoritos,沉没,香蕉,Aeman

但它应该看起来像

阿里托斯,沉没,香蕉,阿曼

该要求不允许使用任何内置函数,并且实际程序要求字母不区分大小写,但我可以通过替换代码并添加一对 if else 来处理它,我只需要帮助更改索引 0 条件。任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 你的例子在我看来不妥,请更正。

标签: javascript arrays function conditional-statements


【解决方案1】:

您可以检查您的输入字符是否在第一个索引str.indexOf(char) 然后只需将前缀"A" 添加到您的字符串without first charstr.substring(1)

function capital(arr, char) {
  let str = "";
  let result = "";
  for (let i = 0; i < arr.length; i++) {
    str = arr[i];
    if(str.indexOf(char) === 0) {
      result = 'A' + str.substring(1);
    }else {
      result = str;
    }

    console.log(result);
  }
}
capital(["doritos", "sandking", "bandana", "demand"], "d");

【讨论】:

    【解决方案2】:

    这个逻辑可以帮助你

    function capital(arr, char) {
    
      return arr.map(e => {
    
        let isFirst = false;
        if (e[0] == "d") isFirst = true;
        e = e.replace(new RegExp(char, 'ig'), '');
        if (isFirst)
          e = e.replace(e.charAt(0), "A");
        return e;
      });
    
    }
    console.log(capital(["doritos", "sandking", "bandana", "demand"], 'd'))

    【讨论】:

      【解决方案3】:

      您可以遍历单词的每个字母并更新第一个字母,如果它与传递的字母匹配并且在 0 索引处,如果它与传递的字母不匹配,则将其他索引添加到您的结果中。

      const capitalize = ch => {
       const letter = ch.charCodeAt(0);
       if(letter >= 97 && letter <= 122) {
          return String.fromCharCode(letter - 32);
       }
       return ch;
      }
      
      const capital = (words, ch) => {
        let result = '';
        for(let i = 0; i < words.length; i++) {
          let newWord = '';
          for(let j = 0; j < words[i].length; j++) {
            if(capitalize(words[i][j]) === capitalize(ch) && j === 0) {
              newWord = 'A';
            }
            if(capitalize(words[i][j]) !== capitalize(ch)) {
              newWord += words[i][j];
            }
          }
          result += newWord;
          if(i < words.length - 1 ) {
            result += ',';
          }
        }
        return result;
      }
      
      const result = capital(["doritos","sandking","bandana", "demand", "sandDking"], "d");
      console.log(result);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-28
        • 1970-01-01
        • 1970-01-01
        • 2017-06-09
        • 2022-01-20
        • 1970-01-01
        相关资源
        最近更新 更多