如何在JavaScript中循环嵌套数组并将其部分添加到具有相同ID的不同div中

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

how to loop over a nested array and add parts of it to different divs with the same id javascript

问题

我有一个嵌套数组的 JSON,我想将其中的部分打印到具有相同 ID 的 div 中。我考虑使用 foreach 循环来遍历嵌套数组。为了将它们添加到每个 div,我考虑给每个 div 添加一个数据集,但当我这样做后,在 foreach 循环之后添加一个 for 循环时,所有数据集都被设置为数组中的最后一个 ID。我该怎么做?
const data = [
    {
        "id": 1,
        "movie_name": "last holiday",
        "date": "2023-10-29",
        "length": ["1 hour 30"],
    },
    {
        "id": 2,
        "movie_name": "pitch perfect",
        "date": "2023-04-24",
        "length": ["2 hours"]
    }
];
<div class="fun">
    <p>Hello</p>
</div>

<div class="fun">
    <p>Hello</p>
</div>
data.forEach(e => {
    for (let i = 0; i < fun.length; i++) {
        fun[i].dataset.id = e.id;
        if (fun[i].dataset.id === e.id) {
            fun[i].insertAdjacentHTML("beforeend",`
            <h4 class="cardTitle">${e.movie_name}</h4>
            <h5 class="cardSubtitle">${e.date}</h5>`);
        }
    }
});
<!-- 期望输出如下: -->

<div class="fun" data-id="1">
    <p>Hello</p>
    <h3>last holiday</h3>
    <h4>2023-10-29</h4>
</div>

<div class="fun" data-id="2">
    <p>Hello</p>
    <h3>pitch perfect</h3>
    <h4>2023-04-24</h4>
</div>

最佳方法是将数据添加到对应的 div 中,而不需要额外的循环。如果你想按 ID 匹配 div,可以使用类似下面的方法:

data.forEach(e => {
    const div = document.querySelector(`.fun[data-id="${e.id}"]`);
    if (div) {
        div.insertAdjacentHTML("beforeend",`
        <h3>${e.movie_name}</h3>
        <h4>${e.date}</h4>`);
    }
});
英文:

I have a json a nested array that I would like to print parts of it to a div that has the same id. I had thought of doing a foreach loop to loop to loop of the nested array. In order to add it to each div I thought about giving each div a dataset but when I do this and then add a for loop after the foreach loop all the data set is being set to the last id in the array. What should I do?

    const data = [
        {
            &quot;id&quot;: 1,
            &quot;movie_name&quot;: &quot;last holiday&quot;,
            &quot;date&quot;: &quot;2023-10-29&quot;,
            &quot;length&quot;: [&quot;1 hour 30&quot;],
        },
        {
            &quot;id&quot;: 2,
            &quot;movie_name&quot;: &quot;pitch perfect&quot;,
            &quot;date&quot;: &quot;2023-04-24&quot;,
            &quot;length&quot;: [&quot;2 hours&quot;]
        }
    ];

&lt;div class=&quot;fun&quot;&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

&lt;div class=&quot;fun&quot;&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;
 data.forEach(e =&gt; {
    for (let i = 0; i &lt; fun.length; i++) {
        fun[i].dataset.id = e.id;
        if (fun[i].dataset.id === e.id) {
    fun[i].insertAdjacentHTML(&quot;beforeend&quot;,`
    &lt;h4 class=&quot;cardTitle&quot;&gt;${e.movie_name}&lt;/h4&gt;
    &lt;h5 class=&quot;cardSubtitle&quot;&gt;${e.date}&lt;/h5&gt;`);
}
    
  }
})

I want the following output to be

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

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

&lt;div class=&quot;fun&quot; data-id=&quot;1&quot;&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;h3&gt;last holiday&lt;/h3&gt;
&lt;h4&gt;2023-10-29&lt;/h4&gt;
&lt;/div&gt;

&lt;div class=&quot;fun&quot; data-id=&quot;2&quot;&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;h3&gt;pitch perfect&lt;/h3&gt;
&lt;h4&gt;2023-04-24&lt;/h4&gt;
&lt;/div&gt;

<!-- end snippet -->

What is the best way to do this

答案1

得分: 1

如果您使用document.querySelectorAll('.fun')来获取所有具有fun类的div,然后您可以循环遍历data并使用该索引将您的内容插入到allFuns[i]中的每个对象。

以下是生成的HTML:

<body>
    <div class="fun" data-id="1">
        <p>Hello</p>

        <h4 class="cardTitle">last holidy</h4>
        <h5 class="cardSubtitle">2023-10-29</h5>
    </div>

    <div class="fun" data-id="2">
        <p>Hello</p>

        <h4 class="cardTitle">pitch perfect</h4>
        <h5 class="cardSubtitle">2023-04-24</h5>
    </div>

    <div class="fun">
        <p>Hello</p>
    </div>
</body>
英文:

If you use document.querySelectorAll(&#39;.fun&#39;) to get all the divs with fun class, you can then loop over data and use that index to insert your content to allFuns[i] for each object.

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

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

const data = [
    {&quot;id&quot;: 1, &quot;movie_name&quot;: &quot;last holidy&quot;, &quot;date&quot;: &quot;2023-10-29&quot;, &quot;length&quot;: [&quot;1 hour 30&quot;], },
    {&quot;id&quot;: 2, &quot;movie_name&quot;: &quot;pitch perfect&quot;, &quot;date&quot;: &quot;2023-04-24&quot;, &quot;length&quot;: [&quot;2 hours&quot;] }
];

const allFuns = document.querySelectorAll(&#39;.fun&#39;);

for (let i in data) {
    let obj = data[i];
    
    allFuns[i].dataset.id = obj.id;
    allFuns[i].insertAdjacentHTML(&quot;beforeend&quot;,`
        &lt;h4 class=&quot;cardTitle&quot;&gt;${obj.movie_name}&lt;/h4&gt;
        &lt;h5 class=&quot;cardSubtitle&quot;&gt;${obj.date}&lt;/h5&gt;
    `);
}

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

&lt;div class=&quot;fun&quot;&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

&lt;div class=&quot;fun&quot;&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

&lt;div class=&quot;fun&quot;&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

This generates the following HTML:

&lt;body&gt;
    &lt;div class=&quot;fun&quot; data-id=&quot;1&quot;&gt;
        &lt;p&gt;Hello&lt;/p&gt;

        &lt;h4 class=&quot;cardTitle&quot;&gt;last holidy&lt;/h4&gt;
        &lt;h5 class=&quot;cardSubtitle&quot;&gt;2023-10-29&lt;/h5&gt;
    &lt;/div&gt;

    &lt;div class=&quot;fun&quot; data-id=&quot;2&quot;&gt;
        &lt;p&gt;Hello&lt;/p&gt;

        &lt;h4 class=&quot;cardTitle&quot;&gt;pitch perfect&lt;/h4&gt;
        &lt;h5 class=&quot;cardSubtitle&quot;&gt;2023-04-24&lt;/h5&gt;
    &lt;/div&gt;

    &lt;div class=&quot;fun&quot;&gt;
        &lt;p&gt;Hello&lt;/p&gt;
    &lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年3月7日 21:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662460.html
匿名

发表评论

匿名网友

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

确定