替换字符串数组中的最后一个字母Javascript

时间:2016-11-18 15:19:46

标签: javascript arrays string substr

我需要做如下:

我有一个包含姓氏的字符串数组。其中一些以字母'i'结尾。

manLastNames = [“testowski”,“bucz”,“idzikowski”,“gosz”];

我需要创建一个迭代这个字符串数组的函数,如果有一个以'i'结尾的元素,我需要将'i'替换为'a',否则只需保留字符串。

最后我想要另一个数组,其中所有最后的'我被'a'替换。

womanLastNames = [“testowska”,“bucz”,“idzikowska”,“gosz”];

这就是我现在所拥有的,但我很确定它在某些时候开始变废话

    var rep = function() {
  var manLastNames = ["testowski","bucz","idzkowski","gosz"];
  var womanLastNames = new Array(4);


  for (var i=0; i<manLastNames.length; i++) {
    var lastName = manLastNames[i];
    if (lastName.substr(lastName.length - 1, 1) == 'i') {
        lastName = lastName.substr(0, lastName.length - 1) + 'a';
  }
  }

  for (var i=0; i<womanLastNames.length; i++) {
    womanLastNames[i] = lastName[i];

  }

  console.log(womanLastNames);
}
rep();

5 个答案:

答案 0 :(得分:4)

尝试代码:

&#13;
&#13;
var manNames =  ["testowski","bucz","idzkowski","gosz"];
var womanNames = manNames.map(function(name) { 
    return name.endsWith("i") ? name.slice(0, -1) + "a" : name; 
});
console.log(womanNames)
&#13;
&#13;
&#13;

如果您的口译员支持ES6,则以下内容相同:

names.map((name)=>name.endsWith("i") ? name.slice(0, -1) + "a" : name)

答案 1 :(得分:1)

这是解决方案

&#13;
&#13;
var rep = function() {
  var manLastNames = ["testowski","bucz","idzkowski","gosz"];
  var womanLastNames =[];

  for (var i=0; i<manLastNames.length; i++) {
    var lastName = manLastNames[i];
    if (lastName.charAt(lastName.length - 1) == 'i') {
        lastName = lastName.substr(0, lastName.length - 1) + 'a';
   }
   womanLastNames.push(lastName);
  }
  console.log(womanLastNames);
}
rep();
&#13;
&#13;
&#13;

另一个解决方案是使用.map这样的方法,使用回调函数:

var manLastNames = ["testowski","bucz","idzikowski","gosz"];
function mapNames(item){
   return item[item.length-1]=='i' ? item.substr(0, item.length-1) + "a" : item;
}
console.log(manLastNames.map(mapNames));

答案 2 :(得分:1)

根据您需要的效率,您可以使用正则表达式执行这两项任务:

var new_name = name.replace(/i$/, 'a');

将字符串中的最后一个“i”替换为“a”(如果存在)

var new_name = name.replace(/i/g, 'a');

将用“a”替换字符串中的所有“i”。

var names = ["testowski", "bucz", "idzkowski", "gosz"];
console.log("original", names);

var last_i_replaced = names.map(function(name) {
  return name.replace(/i$/, 'a');
});
console.log("last 'i' replaced", last_i_replaced);

var all_i_replaced = names.map(function(name) {
  return name.replace(/i/g, 'a');
});
console.log("all 'i's replaced", all_i_replaced);

答案 3 :(得分:0)

这应该有效:

    var rep = function() {
  var manLastNames = ["testowski","bucz","idzkowski","gosz"];
  var womanLastNames = manLastNames;
  for(var i=0; i<manLastNames.length;i++){
    if(manLastNames[i].charAt(manLastNames[i].length-1)=='i'){
      womanLastNames[i]=manLastNames[i].substr(0,womanLastNames[i].length-1)+'a';
    }
  }
  console.log(womanLastNames);
}
rep();

答案 4 :(得分:0)

这是另一种解决方案

var manLastNames = ["testowski","bucz","idzkowski","gosz"];
var womanLastNames = []
manLastNames.forEach(x => {
  if (x.charAt(x.length-1) === "i") womanLastNames.push(x.slice(0,-1).concat("a"));
  else womanLastNames.push(x);
});

console.log(womanLastNames);