目標:使用陣列方法洗掉字串get, right, the, first,time并將它們替換為 secretMessage 陣列中已知的單個字串。
計劃定義一個函式來處理雨中的文本、舊字串和新字串,并使用.replaceall()方法并一次傳遞每個不同的引數,每次呼叫都重新分配陣列。
到目前為止的代碼
let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
function replaceWords(orginalArray,oldString,updatedString){=
let updatedArray= orginalArray.replaceAll(oldString,updatedString);//Not Sure If I need to wrap this in []
return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');
console.log(secretMessage);
當前結果
let updatedArray= orginalArray.replaceAll(oldString,updatedString);
^
型別錯誤:
orginalArray.replaceAll 不是 replaceWords 中的函式
uj5u.com熱心網友回復:
只是為了“有趣”,同時使用陣列方法 ( Array.prototype.join())和 replaceAll()( String.prototype.replaceAll()) 并接近您最初想法的解決方案可能是:
function replaceWords(arr, oldStr, newStr) {
return arr.join(' ') // join all the array items into a single space-separated string
.replaceAll(oldStr, newStr) // call `replaceAll()` on the newly created string
.split(' '); // split the string back into an array
}
let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
secretMessage = replaceWords(secretMessage, 'get', 'know');
secretMessage = replaceWords(secretMessage, 'right', 'know');
secretMessage = replaceWords(secretMessage, 'the', 'know');
secretMessage = replaceWords(secretMessage, 'first', 'know');
secretMessage = replaceWords(secretMessage, 'time', 'know');
console.log(secretMessage);
話雖如此,我認為在現實生活中沒有人會這樣做;)
uj5u.com熱心網友回復:
正如@Pointy 的評論所述,replaceAll()是一個字串方法,它在陣列上不可用。
所以我們需要一種方法來在每個陣列索引上應用一些東西。
幸運的是map(),正是這樣做的:
該
map()方法創建一個新陣列,其中填充了對呼叫陣列中的每個元素呼叫提供的函式的結果。
let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
function replaceWords(orginalArray,oldString,updatedString){
return orginalArray.map(item => {
return item.replaceAll(oldString, updatedString)
});
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');
console.log(secretMessage);
uj5u.com熱心網友回復:
replaceAll是字串函式,而不是陣列函式。如果要替換可能作為陣列中的值存在的單詞,則必須遍歷該陣列。您的代碼如下所示:
let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
function replaceWords(orginalArray,oldString,updatedString){
// because your array-values are just single words, I only use string-comparison, not replaceAll()
let updatedArray= orginalArray.map(string => (string == oldString ? updatedString : string));
return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');
console.log(secretMessage);
uj5u.com熱心網友回復:
您可以擴展陣列功能以滿足您的要求。但不推薦。
只需在陣列宣告之前添加以下代碼,您的代碼就可以作業了。
Array.prototype.replaceAll = function(oldString, updatedString){
return this.slice().map((str)=>((str === oldString)?updatedString:str));
}
uj5u.com熱心網友回復:
replaceAll()適用于字串而不是陣列。試試下面的例子。
let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
function replaceWords(orginalArray,oldString,updatedString){=
let updatedArray= orginalArray.join().replaceAll(oldString,updatedString).split();
return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');
console.log(secretMessage);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399283.html
標籤:javascript 细绳 代替 全部替换
上一篇:從文本字串中提取最大數字
