问题循环遍历 JavaScript 中的数组中的所有元素

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

Issue looping through all of the elements in an Array JavaScript

问题

以下是您要翻译的代码部分:

  1. let myArray = [
  2. {myParagraph: "This is paragraph 1"},
  3. {myParagraph: "This is paragraph 2"},
  4. {myParagraph: "This is paragraph 3"},
  5. {myParagraph: "This is paragraph 4"},
  6. {myParagraph: "This is paragraph 5"},
  7. ]
  8. let page = 1;
  9. let button_number = 1;
  10. let first_element_shown = 0;
  11. let last_element_shown = 10;
  12. let number_of_pages = Math.ceil(myArray.length / 10);
  13. for (let i of myArray) {
  14. for (let j = 0; j < number_of_pages; j++) {
  15. for (let k = first_element_shown; k < last_element_shown; k++) {
  16. let card = document.createElement('div');
  17. let myParagraph = document.createElement('p');
  18. myParagraph.innerText = i.myParagraph;
  19. card.appendChild('myParagraph');
  20. document.getElementById("example_DIV").appendChild(card);
  21. }
  22. }
  23. }
英文:

I have an Array of objects:

  1. let myArray = [
  2. {myParagraph: &quot;This is paragraph 1&quot;},
  3. {myParagraph: &quot;This is paragraph 2&quot;},
  4. {myParagraph: &quot;This is paragraph 3&quot;},
  5. {myParagraph: &quot;This is paragraph 4&quot;},
  6. {myParagraph: &quot;This is paragraph 5&quot;},
  7. ]

I want to loop through and display all of these objects. These objects should be paginated. To do this, I used this code:

  1. let page = 1;
  2. let button_number = 1;
  3. let first_element_shown = 0;
  4. let last_element_shown = 10;
  5. let number_of_pages = Math.ceil(myArray.length / 10);
  6. for (let i of myArray) {
  7. for (let j = 0; j &lt; number_of_pages; j++) {
  8. for (let k = first_element_shown; k &lt; last_element_shown; k++) {
  9. let card = document.createElement(&#39;div&#39;)
  10. let myParagraph = document.createElement(&#39;p&#39;);
  11. myParagraph.innerText = i.myParagraph;
  12. card.appendChild(&#39;myParagraph&#39;)
  13. document.getElementById(&quot;example_DIV&quot;).appendChild(card);
  14. }
  15. }
  16. }

However, this loops through and displays the same objects multiple times. Is there a way to loop through all of the objects in myArray without using the for (let i of myArray) line in order to stop the same objects from showing multiple times.

答案1

得分: 2

你需要设置以下内容:

  1. pageSize
  2. page
  3. start 等于 (page - 1) * pageSize
  4. end 等于 start + pageSize
  5. 使用 slice 方法来切分数组中显示的部分。

这是一个示例。

  1. let myArray = [{
  2. myParagraph: "This is paragraph 1"
  3. },
  4. {
  5. myParagraph: "This is paragraph 2"
  6. },
  7. {
  8. myParagraph: "This is paragraph 3"
  9. },
  10. {
  11. myParagraph: "This is paragraph 4"
  12. },
  13. {
  14. myParagraph: "This is paragraph 5"
  15. },
  16. {
  17. myParagraph: "This is paragraph 6"
  18. },
  19. {
  20. myParagraph: "This is paragraph 7"
  21. },
  22. {
  23. myParagraph: "This is paragraph 8"
  24. },
  25. {
  26. myParagraph: "This is paragraph 9"
  27. },
  28. {
  29. myParagraph: "This is paragraph 10"
  30. },
  31. {
  32. myParagraph: "This is paragraph 11"
  33. },
  34. {
  35. myParagraph: "This is paragraph 12"
  36. },
  37. {
  38. myParagraph: "This is paragraph 13"
  39. },
  40. {
  41. myParagraph: "This is paragraph 14"
  42. },
  43. ]
  44. let pageSize = 3;
  45. let page = 1;
  46. const elementsContainer = document.querySelector('.elements-container');
  47. const next = document.querySelector('#next');
  48. const prev = document.querySelector('#prev');
  49. const changePageNumber = (pageValue) => {
  50. console.log(pageValue)
  51. let page = pageValue;
  52. let start = (page - 1) * pageSize;
  53. let end = start + pageSize;
  54. let shownElements = myArray.slice(start, end);
  55. elementsContainer.innerHTML = '';
  56. shownElements.forEach(item => {
  57. const div = document.createElement('div');
  58. div.textContent = item.myParagraph;
  59. elementsContainer.append(div);
  60. })
  61. }
  62. next.addEventListener('click', () => changePageNumber(++page));
  63. prev.addEventListener('click', () => changePageNumber(--page));
  64. changePageNumber(page)
  1. .elements-container {
  2. margin-top: 20px;
  3. border: 1px solid red;
  4. }
  1. <button id="next">Next</button>
  2. <button id="prev">Previous</button>
  3. <div class="elements-container">
  4. </div>
英文:

You need to set

  1. pageSize
  2. page
  3. start equal to (page - 1) * pageSize
  4. end equal to start + pageSize
  5. use slice method to slice the shown part in the array.

This is an example.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let myArray = [{
  2. myParagraph: &quot;This is paragraph 1&quot;
  3. },
  4. {
  5. myParagraph: &quot;This is paragraph 2&quot;
  6. },
  7. {
  8. myParagraph: &quot;This is paragraph 3&quot;
  9. },
  10. {
  11. myParagraph: &quot;This is paragraph 4&quot;
  12. },
  13. {
  14. myParagraph: &quot;This is paragraph 5&quot;
  15. },
  16. {
  17. myParagraph: &quot;This is paragraph 6&quot;
  18. },
  19. {
  20. myParagraph: &quot;This is paragraph 7&quot;
  21. },
  22. {
  23. myParagraph: &quot;This is paragraph 8&quot;
  24. },
  25. {
  26. myParagraph: &quot;This is paragraph 9&quot;
  27. },
  28. {
  29. myParagraph: &quot;This is paragraph 10&quot;
  30. },
  31. {
  32. myParagraph: &quot;This is paragraph 11&quot;
  33. },
  34. {
  35. myParagraph: &quot;This is paragraph 12&quot;
  36. },
  37. {
  38. myParagraph: &quot;This is paragraph 13&quot;
  39. },
  40. {
  41. myParagraph: &quot;This is paragraph 14&quot;
  42. },
  43. ]
  44. let pageSize = 3;
  45. let page = 1;
  46. const elementsContainer = document.querySelector(&#39;.elements-container&#39;);
  47. const next = document.querySelector(&#39;#next&#39;);
  48. const prev = document.querySelector(&#39;#prev&#39;);
  49. const changePageNumber = (pageValue) =&gt; {
  50. console.log(pageValue)
  51. let page = pageValue;
  52. let start = (page - 1) * pageSize;
  53. let end = start + pageSize;
  54. let shownElements = myArray.slice(start, end);
  55. elementsContainer.innerHTML = &#39;&#39;;
  56. shownElements.forEach(item =&gt; {
  57. const div = document.createElement(&#39;div&#39;);
  58. div.textContent = item.myParagraph;
  59. elementsContainer.append(div);
  60. })
  61. }
  62. next.addEventListener(&#39;click&#39;, () =&gt; changePageNumber(++page));
  63. prev.addEventListener(&#39;click&#39;, () =&gt; changePageNumber(--page));
  64. changePageNumber(page)

<!-- language: lang-css -->

  1. .elements-container {
  2. margin-top: 20px;
  3. border: 1px solid red;
  4. }

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

  1. &lt;button id=&quot;next&quot;&gt;Next&lt;/button&gt;
  2. &lt;button id=&quot;prev&quot;&gt;Previous&lt;/button&gt;
  3. &lt;div class=&quot;elements-container&quot;&gt;
  4. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

  1. let page = 1;
  2. let button_number = 1;
  3. let count = 0;
  4. let first_element_shown = 0;
  5. let last_element_shown = 10;
  6. let number_of_pages = Math.ceil(myArray.length / 10);
  7. for (let j = 0; j &lt; number_of_pages; j++) {
  8. for (let k = first_element_shown; k &lt; last_element_shown &amp; count&lt;myArray.length; k++) {
  9. let card = document.createElement(&#39;div&#39;);
  10. let myParagraph = document.createElement(&#39;p&#39;);
  11. myParagraph.innerText = myArray[count].myParagraph;
  12. card.appendChild(myParagraph);
  13. document.getElementById(&quot;example_DIV&quot;).appendChild(card);
  14. count++;
  15. }
  16. }

以上是您提供的代码的翻译部分。

英文:
  1. let page = 1;
  2. let button_number = 1;
  3. let count = 0;
  4. let first_element_shown = 0;
  5. let last_element_shown = 10;
  6. let number_of_pages = Math.ceil(myArray.length / 10);
  7. for (let j = 0; j &lt; number_of_pages; j++) {
  8. for (let k = first_element_shown; k &lt; last_element_shown &amp; count&lt;myArray.length; k++) {
  9. let card = document.createElement(&#39;div&#39;)
  10. let myParagraph = document.createElement(&#39;p&#39;);
  11. myParagraph.innerText = myArray[count].myParagraph;
  12. card.appendChild(myParagraph)
  13. document.getElementById(&quot;example_DIV&quot;).appendChild(card);
  14. count++
  15. }
  16. }

While this isn't perfect, it fixes some of your issues and you can probably figure out the rest from there.

huangapple
  • 本文由 发表于 2023年2月16日 19:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471491.html
匿名

发表评论

匿名网友

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

确定