如何提取对象内部的数组

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

How to extract the array inside the object

问题

const html = test.map((user) => {
    return `
    <div class="user">
    ID: ${user.labels.id}
    </div>
    `;
document.querySelector("#app").insertAdjacentHTML("afterbegin", html);

API URL: https://api.github.com/repos/vercel/next.js/issues

There's an array inside the object which is labels. And I want to try to extract the object inside "labels" array. But since the method I use only work if it's an object, how to do it if it's an array?


<details>
<summary>英文:</summary>

const html = test.map((user) => {
return
&lt;div class=&quot;user&quot;&gt;
ID: ${user.labels.id}
&lt;/div&gt;
;
document.querySelector("#app").insertAdjacentHTML("afterbegin", html);


API URL : https://api.github.com/repos/vercel/next.js/issues

There&#39;s an array inside the object which is labels. And I want to try to extract the object inside &quot;labels&quot; array. But since the method I use only work if it&#39;s an object, how to do it if it&#39;s an array?

</details>


# 答案1
**得分**: -1

const html = 
test.map((user) => {
  return `
    ${user.labels.map((label) => {
      return `
        <div class="label">
          Labels:<br>
          ID: ${label.id}<br>
          Node ID: ${label.node_id}<br>
        </div>
      `;
    })}
  </p>
  </div>
  `;
})
.join("");
console.log(html);
document.querySelector("#app").insertAdjacentHTML("afterbegin", html);

<details>
<summary>英文:</summary>

    const html = 
    test.map((user) =&gt; {
    return `
    ${user.labels.map((label) =&gt; {
    return `
    &lt;div class=&quot;label&quot;&gt;
    Labels:&lt;br&gt;
    ID: ${label.id}&lt;br&gt;
    Node ID: ${label.node_id}&lt;br&gt;
    &lt;/div&gt;
    `;
    })}
    &lt;/p&gt;
    &lt;/div&gt;
    `;
    })
    .join(&quot;&quot;);
    console.log(html);
    document.querySelector(&quot;#app&quot;).insertAdjacentHTML(&quot;afterbegin&quot;, html);

</details>



huangapple
  • 本文由 发表于 2023年5月26日 09:46:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337179.html
匿名

发表评论

匿名网友

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

确定