删除包含数组中包含特定字母的元素
我有这样的数组。
const cntry = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Turkey', 'Iceland', 'Ireland'];
我想从此数组中删除包含“ land”一词的元素,但是由于我正在研究“ for Loop”,因此我只想使用“ for”方法将其删除。
我尝试了此代码,但它不起作用。 “爱尔兰”仍在阵列中。我哪里出错了?
for (let i = 0; i < cntry.length; i++) {
if (cntry[i].includes('land')) {
cntry.splice(i, 1);
}
}
输出:
(9) ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Germany', 'Hungary', 'Turkey', 'Ireland']
I have array like this.
const cntry = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Turkey', 'Iceland', 'Ireland'];
I want to delete elements that contain the word "land" from this array but since I am studying "for loop" i want to remove it using only the "for" method.
I tried this code but it didn't work. "Ireland" is still in the array. where did I go wrong?
for (let i = 0; i < cntry.length; i++) {
if (cntry[i].includes('land')) {
cntry.splice(i, 1);
}
}
Output:
(9) ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Germany', 'Hungary', 'Turkey', 'Ireland']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在突变
cntry
当您 循环。当您从数组中删除元素时,这会打破所有其余元素的增量/索引关系。因此,相反,将循环扭转 - 从数组的末端开始,然后开始工作:现在,当您删除元素时,前面的索引仍然与您当前的增量值相对应。
详细说明:
for(让i = cntry.length-1; i&gt; = 0; i-)
在您的循环中,将增量变量设置为
i
在数组负1中。由于数组为零索引,这意味着第一个元素为索引0
,您需要以阵列减1的长度开始增量。数组因此,最后一个元素为索引10
。然后允许循环
i
更大或等于&gt; =
0。换句话说,当i
小于零:最后,
降低 。在评估循环块和下一次迭代之前,发生循环增量变化发生。
在“现实世界”中,您将使用一个非常适合您希望评估的代码块的循环结构。由于您正在突变阵列,因此更适合
foreach
循环由于它不取决于维持增量/索引关系。也许这是您的作业目的,向您展示何时不合适的循环。You're mutating
cntry
as youfor
loop it. This breaks the increment/index relationship of all the remaining elements as soon as you remove an element from the array. So instead, reverse the loop—start from the end of the array and work toward the start:Now when you remove an element, the preceding indexes still correspond to your current increment value.
Elaborating on:
for (let i = cntry.length - 1; i >= 0; i--)
In your for loop, set increment variable
i
to the length of the array minus 1. Since arrays are zero indexed, meaning the first element is indexed0
, you'll need to start your increment at the length of the array minus 1. There are 11 elements in your array so the last element is indexed10
.Then allow loop while
i
is greater to or equal to>=
0. In other words, leave the loop wheni
is less than zero:And finally,
decrement
i
. The for loop increment change happens after the loop block has been evaluated and before the next iteration.In the "real world" you'd use a loop construct that is well suited to the block of code you wish to evaluate. Since you are mutating the array it would be more suitable to
forEach
loop since it does not depend on maintaining an increment/index relationship. Maybe that's the purpose of your homework assignment, to show you when a for loop is not appropriate.您可以
filter
您的列表,因为当您使用slice
时,您正在覆盖原始数组:You can
filter
your list because when you useslice
you are overwriting the original array:已经有一个更好的答案可以利用现有数组,但这是一种替代解决方案。
There is already a better answer that utilizes the existing array but here is an alternative solution.