首页 > 解决方案 > 对象数组过滤并获取另一个结构中的值

问题描述

我将数据作为对象数组,我应该过滤数据并获取具有值的对象和最小键,我尝试使用循环和过滤器,但我无法达到预期的效果。我已经分享了我拥有的数据和预期的结构,找到下面的代码,请帮助我。

我在下面的数据:

var data = [
  {
    "Id": 1392236,
    "foldId": 410176,
    "binding": [
      {
        "assayType": "binding",
        "activityValue": 0.03,
        "strId": 1392236
      },
      {
        "assayType": "binding",
        "activityValue": 5.0,
        "strId": 1392236
      }
    ],
    "functional invitro": [
      {
        "assayType": "functional invitro",
        "activityValue": 0.03,
        "strId": 1392236
      },
      {
        "assayType": "functional invitro",
        "activityValue": 5.0,
        "strId": 1392236
      },
      
    ],
    "functionalInvivo": [
      {
        "assayType": "functional invivo",
        "activityValue": 45.0,
        "strId": 1392236
      },
      {
        "assayType": "functional invivo",
        "activityValue": 54.0,
        "strId": 1392236
      }
    ],
    "pharmacokinetics": [
      {
        "assayType": "pharmacokinetics",
        "activityValue": 10.67,
        "strId": 1392236
      },
      {
        "assayType": "pharmacokinetics",
        "activityValue": 2.6,
        "strId": 1392236
      }          
    ]
  },
  {
    "Id": 1392543,
    "foldId": 410176,
    "binding": [
      {
        "assayType": "binding",
        "activityValue": 0.38,
        "strId": 1392543
      },
      {
        "assayType": "binding",
        "activityValue": 4.3,
        "strId": 1392543
      }
      
    ],
    "functional invitro": [
      {
        "assayType": "functional invitro",
        "activityValue": 2.03,
        "strId": 1392543
      },
      {
        "assayType": "functional invitro",
        "activityValue": 3.0,
        "strId": 1392543
      },
      
    ],
    "functionalInvivo": [
      {
        "assayType": "functional invivo",
        "activityValue": 25.0,
        "strId": 1392543
      },
      {
        "assayType": "functional invivo",
        "activityValue": 64.0,
        "strId": 1392543
      }
    ],
    "pharmacokinetics": [
      {
        "assayType": "pharmacokinetics",
        "activityValue": 30.67,
        "strId": 1392543
      },
      {
        "assayType": "pharmacokinetics",
        "activityValue": 5.6,
        "strId": 1392543
      }          
    ]
  }
]

过滤后期望如下:

var filterData = [
  {
      "assayType": "binding",
      "activityValue": 0.03,
      "strId": 1392543
  },
  {
      "assayType": "functional invitro",
      "activityValue": 5.0,
      "strId": 1392543
  },
  {
      "assayType": "binding",
      "activityValue": 4.3,
      "strId": 1392236
  },
  {
      "assayType": "pharmacokinetics",
      "activityValue": 6.06,
      "strId": 1392543
  },
  {
      "assayType": "functional invivo",
      "activityValue": 64,
      "strId": 1392543
  },
  {
      "assayType": "functional invivo",
      "activityValue": 4,
      "strId": 1392236
  },
  {
      "assayType": "pharmacokinetics",
      "activityValue": 6.06,
      "strId": 1392236
  },
]

帮我解决。

标签: javascriptarraysobjectecmascript-6filter

解决方案


您可以为此使用filtermapflat方法,

data.map((item) => {
  return Object.keys(item).filter(key=> item[key].length).map((key) => item[key]).flat()
}).flat()

如果您需要进一步过滤数据,您可以在平面数组上执行此操作。


推荐阅读