如何提取对象内部的数组

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

How to extract the array inside the object

问题

  1. const html = test.map((user) => {
  2. return `
  3. <div class="user">
  4. ID: ${user.labels.id}
  5. </div>
  6. `;
  7. 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?

  1. <details>
  2. <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);

  1. API URL : https://api.github.com/repos/vercel/next.js/issues
  2. 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?
  3. </details>
  4. # 答案1
  5. **得分**: -1
  6. const html =
  7. test.map((user) => {
  8. return `
  9. ${user.labels.map((label) => {
  10. return `
  11. <div class="label">
  12. Labels:<br>
  13. ID: ${label.id}<br>
  14. Node ID: ${label.node_id}<br>
  15. </div>
  16. `;
  17. })}
  18. </p>
  19. </div>
  20. `;
  21. })
  22. .join("");
  23. console.log(html);
  24. document.querySelector("#app").insertAdjacentHTML("afterbegin", html);
  25. <details>
  26. <summary>英文:</summary>
  27. const html =
  28. test.map((user) =&gt; {
  29. return `
  30. ${user.labels.map((label) =&gt; {
  31. return `
  32. &lt;div class=&quot;label&quot;&gt;
  33. Labels:&lt;br&gt;
  34. ID: ${label.id}&lt;br&gt;
  35. Node ID: ${label.node_id}&lt;br&gt;
  36. &lt;/div&gt;
  37. `;
  38. })}
  39. &lt;/p&gt;
  40. &lt;/div&gt;
  41. `;
  42. })
  43. .join(&quot;&quot;);
  44. console.log(html);
  45. document.querySelector(&quot;#app&quot;).insertAdjacentHTML(&quot;afterbegin&quot;, html);
  46. </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:

确定