我有多個陣列看起來或多或少像這樣:
let r = [
'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 20% for 12s.',
'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 25% for 12s.',
'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 30% for 12s.',
'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 35% for 12s.',
'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 40% for 12s.'
]
let r1 = [
'Increases Movement SPD by 10%. When in battle, gain an 8% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.',
'Increases Movement SPD by 10%. When in battle, earn an 8% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.',
'Increases Movement SPD by 10%. When in battle, earn a 10% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.',
'Increases Movement SPD by 10%. When in battle, earn a 12% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.',
'Increases Movement SPD by 10%. When in battle, earn a 14% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.'
]
問題是我希望陣列中的其他元素(不包括第一個)只包含與其他元素不同的單詞。
// <number>% //
明確地說,我希望接下來的所有行都將替換為//除了更改的單詞之外的所有內容。最后,我想要這樣的結果:
[
'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 20% for 12s.',
'// 20% //',
'// 30% //',
'// 35% //',
'// 40% //'
]
我完全不知道如何實作這一點,我想幫助創建一個函式,因此當我將這樣的陣列作為輸入時,會回傳上面的結果。
uj5u.com熱心網友回復:
您可以嘗試以下功能:
function replaceStatic(array, replacement) {
let currSentence = array[0]; // First comparison sentence
let splittedCurrSentence = currSentence.split(" "); // Get currSentence words in an array (for further comparison)
const newArray = [currSentence]; // The array to return. First sentence can already be in there
for (let s = 1, len = array.length; s < len; s ) { // Loop from second to last sentence
const sentence = array[s]; // Current sentence to analyse
const splittedSentence = sentence.split(" "); // Words array for the sentence to analyze
let replace = true; // Set a boolean to handle when you need to replace the current word
const mappedSplittedSentence = splittedSentence.map(word => {
// Maps every words with itself (if new), the replacement string (if not new a last word was new) or doesn't map
if (!splittedCurrSentence.includes(word)) {
replace = true;
return word;
} else if (replace) {
replace = false; // Set to false so if next word is also new, there won't be consecutive replacements
return replacement;
}
}).filter(word => word !== undefined); // Remove unmapped words (consecutive already existing words)
newArray.push(mappedSplittedSentence.join(" ")); // Stringify the mapped sentence
// Set comparison sentence to the current sentence for next iteration
// This is done so every check is made compared to previous sentence
// You can comment/remove this if you only want to compare to the first sentence
splittedCurrSentence = splittedSentence;
}
return newArray;
}
如果你呼叫你的 r 陣列,它會回傳:
console.log( replaceStatic(r, "//") );
/*
Output:
[
'Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by 20% for 12s.',
'// 25% //',
'// 30% //',
'// 35% //',
'// 40% //'
]
*/
使用 r1 它給出:
console.log( replaceStatic(r1, "//") );
/*
Output:
[
'Increases Movement SPD by 10%. When in battle, gain an 8% Elemental DMG Bonus every 4s. Max 4 stacks. Lasts until the character falls or leaves combat.',
'// earn //',
'// a 10% //',
'// 12% //',
'// 14% //'
]
*/
可能有更好的解決方案,但這應該足夠通用以適合您的情況。
uj5u.com熱心網友回復:
我將從具有要替換的特定短語的“模板字串”(只是一個名稱,而不是真實的模板文字)和要替換它的值陣列開始,然后將替換值映射到模板字串。
const templateString = "Upon causing an Overloaded, Melt, Burning, Vaporize, or a Pyro-infused Swirl reaction, increases Base ATK by **percent**% for 12s.";
const percentages = [20,25,30,40];
const replacedStrings = percentages.map(
function(p) {
return templateString.replace("**percent**",p);
}
);
console.log("replacedStrings",replacedStrings);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/344690.html
標籤:javascript 节点.js 数组 正则表达式 功能
上一篇:一一請求URL
