能否同时对多字符串使用includes与replace?批量修正足球API数据
Hey there! Those repetitive if statements are definitely a pain to maintain—great call looking for a batch solution. Here are two clean, scalable ways to replace all those terms without repeating yourself:
Batch Replace Team Name Cleanup
Option 1: Use an Array of Terms (Readable & Easy to Update)
This approach keeps all the strings you want to strip in a single list, making it trivial to add or remove terms later without rewriting core logic.
// List all the strings you need to remove from team names const termsToRemove = ["Rotterdam", "'65", "Almelo"]; // Loop through each term and replace it (no need for includes—replace does nothing if the term isn't present) termsToRemove.forEach(term => { el.team.name = el.team.name.replace(term, ""); });
If removing terms leaves extra spaces (like turning "Feyenoord Rotterdam" into "Feyenoord "), add a trim() to clean things up:
termsToRemove.forEach(term => { el.team.name = el.team.name.replace(term, "").trim(); });
Option 2: Use a Regular Expression (Single Pass)
For a more concise solution, combine all terms into a regex pattern. This runs one replacement operation instead of multiple loops:
// Regex matches any of the terms (add 'i' flag at the end for case-insensitive matching) const removePattern = /Rotterdam|'65|Almelo/g; el.team.name = el.team.name.replace(removePattern, "").trim();
Why These Are Better Than Repeated if Checks
- Maintainability: Adding a new term only requires updating the array or regex, not writing a whole new
ifblock. - Readability: Anyone looking at your code can immediately see all the terms being removed in one place.
- Less Boilerplate: Cuts down on redundant code that does the same thing over and over.
内容的提问来源于stack exchange,提问作者Noud
相关产品推荐
相关产品推荐

