2016-11-18 191 views
0

我需要做如下操作:替換字符串數組中的最後一個字母Javascript

我有一個包含姓氏的字符串數組。其中一些以字母'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(); 
+1

[如何更換在JavaScript字符串的所有出現?(HTTP的可能重複:// stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) –

回答

1

這裏是解決

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();

另一種解決方案是使用.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)); 
4

嘗試代碼:

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)

如果你的解釋支持ES6,下面是等效的:

names.map((name)=>name.endsWith("i") ? name.slice(0, -1) + "a" : name) 
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();

1

取決於你需要如何高效的是,你可以使用正則表達式來完成這兩項任務:

var new_name = name.replace(/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);

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);

相關問題