[英]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.