【问题标题】:How can I iterate through array of deeply nested objects in javascript?如何遍历 javascript 中深度嵌套的对象数组?
【发布时间】:2020-01-26 04:18:07
【问题描述】:

如何在 javascript 中遍历深度嵌套的对象数组?我想遍历对象的结果数组。
像这样的:

{
      "meta": {
        "disclaimer": "Do not rely on openFDA to make decisions 
         regarding medical care. While we make every effort to 
     ensure that data is accurate, you should assume all results are 
     unvalidated. We may limit or otherwise restrict your access to 
     the API in line with our Terms of Service.",
     "terms": "https://open.fda.gov/terms/",
     "license": "https://open.fda.gov/license/",
     "last_updated": "2019-12-14",
     "results": {
          "skip": 0,
          "limit": 1,
          "total": 504
        }
     },
       **"results": [**
       {
         "effective_time": "20190502",
          "inactive_ingredient": [
           "Inactive ingredients D&C yellow #10, FD&C yellow #6
        ]

【问题讨论】:

  • 您的自己的研究/努力在哪里?你想通过迭代来实现什么?换句话说,你期待什么结果?

标签: javascript api object nested call


【解决方案1】:

考虑到这是您的对象,您可以轻松使用forEach()map() 来遍历results 数组中的对象,具体取决于您的需要。

以截图为例:

Code

希望对你有帮助!

【讨论】:

  • 添加一些代码结果会很有帮助。
  • 这就是我迭代的方式:{drugs.map(drug => (
  • Effective Date
    {drug.effective_time }
  • 通用名称
    {drug.openfda.generic_name}
  • 品牌名称
    {drug.openfda.brand_name}
  • ))}
猜你喜欢
相关资源
最近更新 更多
热门标签