Javascript: for循环无法处理数组中的所有元素

huangapple go评论79阅读模式
英文:

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 = [ &quot;&lt;p&gt;This is a paragraph 1&lt;/p&gt;&quot;, 
                &quot;&lt;p&gt;This is a paragraph 2&lt;/p&gt;&quot;, 
                &quot;&lt;p&gt;This is a paragraph 3&lt;/p&gt;&quot;, 
                &quot;&lt;p&gt;This is a paragraph 4&lt;/p&gt;&quot;, 
                &quot;&lt;p&gt;This is a paragraph 5&lt;/p&gt;&quot; ]

for (var i = content.length-1; i &gt;= 0 ; i--) {
  console.log(&quot;Current item: &quot; + content[i])

  var itemtoRemove = content[i];
  $(&quot;#cell1 .container&quot;).append(content[i]);

  content.splice($.inArray(itemtoRemove, content), 1)
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;article&quot;&gt;
  &lt;div id=&quot;cell1&quot; class=&quot;text-block&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月4日 00:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定