简体   繁体   中英

Array of objects filter and get values in another structure

I have data as Array of objects, i should filter the data and get objects and minimal keys with values, i have tried with loops and filter but i couldn't get as expected. I have shared the data which i have and expected structure, find the code below and please help me.

Data which i have below :

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
      }          
    ]
  }
]

Which am expecting after filtering like below :

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
  },
]

Help me to resolve.

You can use filter , map and flat method for this,

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

If you need to further filter data you can do that on the flat array.

You could take some unwanted properties from the objects and iterate the values for mapping the smallest ones.

 const data = [{ Id: 1392236, foldId: 410176, binding: [{ assayType: "binding", activityValue: 0.03, strId: 1392236 }, { assayType: "binding", activityValue: 5, strId: 1392236 }], "functional invitro": [{ assayType: "functional invitro", activityValue: 0.03, strId: 1392236 }, { assayType: "functional invitro", activityValue: 5, strId: 1392236 }], functionalInvivo: [{ assayType: "functional invivo", activityValue: 45, strId: 1392236 }, { assayType: "functional invivo", activityValue: 54, 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, strId: 1392543 }], functionalInvivo: [{ assayType: "functional invivo", activityValue: 25, strId: 1392543 }, { assayType: "functional invivo", activityValue: 64, strId: 1392543 }], pharmacokinetics: [{ assayType: "pharmacokinetics", activityValue: 30.67, strId: 1392543 }, { assayType: "pharmacokinetics", activityValue: 5.6, strId: 1392543 }] }], result = data.flatMap(({ Id, foldId, ...o }) => Object.values(o).map(array => array.reduce((a, b) => a.activityValue < b.activityValue ? a : b) ) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM