[英]Filter an Array of Objects from another Array of Objects with a different nesting structure
[英]Array of objects filter and get values in another structure
我有数据作为对象数组,我应该过滤数据并获取带有值的对象和最小键,我尝试过循环和过滤器,但我无法按预期获得。 我已经分享了我拥有的数据和预期的结构,找到下面的代码,请帮助我。
我有以下数据:
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
},
]
帮我解决。
您可以从对象中获取一些不需要的属性并迭代映射最小属性的值。
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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.