英文:
Javascript: for-statement not processing all elements in array
问题
我有一个包含各种段落的数组:
var content = [
"<p>This is a paragraph 1</p>",
"<p>This is a paragraph 2</p>",
"<p>This is a paragraph 3</p>",
"<p>This is a paragraph 4</p>",
"<p>This is a paragraph 5</p>"
]
我想对数组中的每个元素执行简单的操作。每个段落需要放在一个容器中,然后从数组中移除。以下是相应的HTML:
<div class="article">
<div id="cell1" class="text-block">
<div class="container"></div>
</div>
</div>
以下是我尝试实现上述描述的方式:
for (i = 0; i < content.length; i++) {
console.log("Current item: " + content[i])
//
var itemtoRemove = content[i];
$("#cell1 .container").append(content[i]);
//
content.splice($.inArray(itemtoRemove, content), 1)
}
基本上,一切都运行正常,但是for循环似乎跳过了数组中的每个第二个元素。我无法找出原因,即使查看了类似的情况也不行。我在这里做错了什么?
英文:
I have an array with various paragraphs inside:
var content = [ "<p>This is a paragraph 1</p>",
"<p>This is a paragraph 2</p>",
"<p>This is a paragraph 3</p>",
"<p>This is a paragraph 4</p>",
"<p>This is a paragraph 5</p>" ]
I want to perform a simple operation on each element inside the array. Each paragraph needs to be placed inside a container and then be removed from the array.
This is the corresponding HTML:
<div class="article">
<div id="cell1" class="text-block">
<div class="container"></div>
</div>
</div>
And this is how I am attempting to do what I describe above:
for (i = 0; i < content.length; i++) {
console.log("Current item: " + content[i])
//
var itemtoRemove = content[i];
$("#cell1 .container").append(content[i]);
//
content.splice($.inArray(itemtoRemove, content), 1)
}
I have put everything together in a fiddle as well: https://jsfiddle.net/johschmoll/1mjzu4qg/71/
Basically, everything is working fine, but the for-statement seems to be skipping every second element in the array. I cannot figure out the reason, even after looking at similar cases. What am I doing wrong here?
答案1
得分: 3
Since you are modifying the array as you loop, use a while loop that checks that the array is not empty
console.log("当前项:" + content[0]);
//
var 要移除的项 = content[0];
$("#cell1 .container").append(content[0]);
//
content.shift();
}
请记住,如果您只是要追加所有元素,那么这是不必要的。append()
可以接受一个数组
英文:
Since you are modifying the array as you loop, use a while loop that checks that the array is not empty
while (content.length) {
console.log("Current item: " + content[0])
//
var itemtoRemove = content[0];
$("#cell1 .container").append(content[0]);
//
content.shift();
}
Keeping in mind that if all you are doing is appending all the elements, then this is unnecessary. append()
can accept an array
$("#cell1 .container").append(content.splice(0));
答案2
得分: 1
Here's the translated code:
因为您在每次迭代中修改数组,数组的长度会发生变化,导致跳过某些项目。为避免这个问题,应该从数组的末尾开始循环。
var content = [ "<p>这是段落 1</p>",
"<p>这是段落 2</p>",
"<p>这是段落 3</p>",
"<p>这是段落 4</p>",
"<p>这是段落 5</p>" ];
for (var i = content.length - 1; i >= 0; i--) {
console.log("当前项目:" + content[i]);
var itemToRemove = content[i];
$("#cell1 .container").append(content[i]);
content.splice($.inArray(itemToRemove, content), 1);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="article">
<div id="cell1" class="text-block">
<div class="container"></div>
</div>
</div>
Please let me know if you need further assistance or more translations.
英文:
Because you are modifying the array upon each iteration, the length of the array changes and you wind up skipping items. Instead, loop through the array backwards to avoid the issue.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var content = [ "<p>This is a paragraph 1</p>",
"<p>This is a paragraph 2</p>",
"<p>This is a paragraph 3</p>",
"<p>This is a paragraph 4</p>",
"<p>This is a paragraph 5</p>" ]
for (var i = content.length-1; i >= 0 ; i--) {
console.log("Current item: " + content[i])
var itemtoRemove = content[i];
$("#cell1 .container").append(content[i]);
content.splice($.inArray(itemtoRemove, content), 1)
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="article">
<div id="cell1" class="text-block">
<div class="container"></div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论