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

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

Issue looping through all of the elements in an Array JavaScript

问题

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

let myArray = [
    {myParagraph: "This is paragraph 1"},
    {myParagraph: "This is paragraph 2"},
    {myParagraph: "This is paragraph 3"},
    {myParagraph: "This is paragraph 4"},
    {myParagraph: "This is paragraph 5"},
]

let page = 1;
let button_number = 1;

let first_element_shown = 0;
let last_element_shown = 10;

let number_of_pages = Math.ceil(myArray.length / 10);

for (let i of myArray) {
    for (let j = 0; j < number_of_pages; j++) {
        for (let k = first_element_shown; k < last_element_shown; k++) {
            let card = document.createElement('div');
            let myParagraph = document.createElement('p');
            myParagraph.innerText = i.myParagraph;
            card.appendChild('myParagraph');
            document.getElementById("example_DIV").appendChild(card);
        }
    }
}
英文:

I have an Array of objects:

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

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

let page = 1;
let button_number = 1;

let first_element_shown = 0;
let last_element_shown = 10;

let number_of_pages = Math.ceil(myArray.length / 10);

for (let i of myArray) {
    for (let j = 0; j &lt; number_of_pages; j++) {
        for (let k = first_element_shown; k &lt; last_element_shown; k++) {
            let card = document.createElement(&#39;div&#39;)
            let myParagraph = document.createElement(&#39;p&#39;);
            myParagraph.innerText = i.myParagraph;
            card.appendChild(&#39;myParagraph&#39;)
            document.getElementById(&quot;example_DIV&quot;).appendChild(card);
           }
    }
}

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 方法来切分数组中显示的部分。

这是一个示例。

let myArray = [{
    myParagraph: "This is paragraph 1"
  },
  {
    myParagraph: "This is paragraph 2"
  },
  {
    myParagraph: "This is paragraph 3"
  },
  {
    myParagraph: "This is paragraph 4"
  },
  {
    myParagraph: "This is paragraph 5"
  },
  {
    myParagraph: "This is paragraph 6"
  },
  {
    myParagraph: "This is paragraph 7"
  },
  {
    myParagraph: "This is paragraph 8"
  },
  {
    myParagraph: "This is paragraph 9"
  },
  {
    myParagraph: "This is paragraph 10"
  },
  {
    myParagraph: "This is paragraph 11"
  },
  {
    myParagraph: "This is paragraph 12"
  },
  {
    myParagraph: "This is paragraph 13"
  },
  {
    myParagraph: "This is paragraph 14"
  },
]

let pageSize = 3;
let page = 1;
const elementsContainer = document.querySelector('.elements-container');
const next = document.querySelector('#next');
const prev = document.querySelector('#prev');

const changePageNumber = (pageValue) => {
  console.log(pageValue)
  let page = pageValue;
  let start = (page - 1) * pageSize;
  let end = start + pageSize;
  let shownElements = myArray.slice(start, end);

  elementsContainer.innerHTML = '';

  shownElements.forEach(item => {
    const div = document.createElement('div');
    div.textContent = item.myParagraph;
    elementsContainer.append(div);
  })
}

next.addEventListener('click', () => changePageNumber(++page));
prev.addEventListener('click', () => changePageNumber(--page));
changePageNumber(page)
.elements-container {
  margin-top: 20px;
  border: 1px solid red;
}
<button id="next">Next</button>
<button id="prev">Previous</button>

<div class="elements-container">
</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 -->

let myArray = [{
myParagraph: &quot;This is paragraph 1&quot;
},
{
myParagraph: &quot;This is paragraph 2&quot;
},
{
myParagraph: &quot;This is paragraph 3&quot;
},
{
myParagraph: &quot;This is paragraph 4&quot;
},
{
myParagraph: &quot;This is paragraph 5&quot;
},
{
myParagraph: &quot;This is paragraph 6&quot;
},
{
myParagraph: &quot;This is paragraph 7&quot;
},
{
myParagraph: &quot;This is paragraph 8&quot;
},
{
myParagraph: &quot;This is paragraph 9&quot;
},
{
myParagraph: &quot;This is paragraph 10&quot;
},
{
myParagraph: &quot;This is paragraph 11&quot;
},
{
myParagraph: &quot;This is paragraph 12&quot;
},
{
myParagraph: &quot;This is paragraph 13&quot;
},
{
myParagraph: &quot;This is paragraph 14&quot;
},
]
let pageSize = 3;
let page = 1;
const elementsContainer = document.querySelector(&#39;.elements-container&#39;);
const next = document.querySelector(&#39;#next&#39;);
const prev = document.querySelector(&#39;#prev&#39;);
const changePageNumber = (pageValue) =&gt; {
console.log(pageValue)
let page = pageValue;
let start = (page - 1) * pageSize;
let end = start + pageSize;
let shownElements = myArray.slice(start, end);
elementsContainer.innerHTML = &#39;&#39;;
shownElements.forEach(item =&gt; {
const div = document.createElement(&#39;div&#39;);
div.textContent = item.myParagraph;
elementsContainer.append(div);
})
}
next.addEventListener(&#39;click&#39;, () =&gt; changePageNumber(++page));
prev.addEventListener(&#39;click&#39;, () =&gt; changePageNumber(--page));
changePageNumber(page)

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

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

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

&lt;button id=&quot;next&quot;&gt;Next&lt;/button&gt;
&lt;button id=&quot;prev&quot;&gt;Previous&lt;/button&gt;
&lt;div class=&quot;elements-container&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

let page = 1;
let button_number = 1;
let count = 0;
let first_element_shown = 0;
let last_element_shown = 10;

let number_of_pages = Math.ceil(myArray.length / 10);

for (let j = 0; j &lt; number_of_pages; j++) {
    for (let k = first_element_shown; k &lt; last_element_shown &amp; count&lt;myArray.length; k++) {
        let card = document.createElement(&#39;div&#39;);
        let myParagraph = document.createElement(&#39;p&#39;);
        myParagraph.innerText = myArray[count].myParagraph;
        card.appendChild(myParagraph);
        document.getElementById(&quot;example_DIV&quot;).appendChild(card);
        count++;
    }
}

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

英文:
let page = 1;
let button_number = 1;
let count = 0;
let first_element_shown = 0;
let last_element_shown = 10;
let number_of_pages = Math.ceil(myArray.length / 10);
for (let j = 0; j &lt; number_of_pages; j++) {
for (let k = first_element_shown; k &lt; last_element_shown &amp; count&lt;myArray.length; k++) {
let card = document.createElement(&#39;div&#39;)
let myParagraph = document.createElement(&#39;p&#39;);
myParagraph.innerText = myArray[count].myParagraph;
card.appendChild(myParagraph)
document.getElementById(&quot;example_DIV&quot;).appendChild(card);
count++
}
}

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:

确定