gpt4 book ai didi

javascript - 将 pickBy 和 mapValues 从 lodash 转换为函数

转载 作者:行者123 更新时间:2023-11-30 14:29:37 25 4
gpt4 key购买 nike

我正在尝试将这个使用 pickBy 和 mapValues 的函数从 lodash 转换为不使用 lodash 的函数。

import mapValues from 'lodash/mapValues';
import pickBy from 'lodash/pickBy';

function clinicsWithInvitations(upcomingClinics) {
const upClinics = upcomingClinics.clinics;
const upInvitations = upcomingClinics.invitations;

const getInvitations = id => pickBy(upInvitations, inv => inv.clinicId === id);
const mergeInvitations = (clinic, clinicId) => ({
...clinic,
invitations: getInvitations(clinicId),
});

return mapValues(upClinics, mergeInvitations);
}

我试过了,没用

function clinicsWithInvitations(upcomingClinics) {
const upClinics = upcomingClinics.clinics;
const upInvitations = upcomingClinics.invitations;

const getInvitations = clinicId =>
Object.keys(upInvitations).reduce((acc, invId) => {
const inv = upInvitations[invId];
return inv.clinicId === clinicId ? { ...acc, [inv.clinicId]: inv } : acc;
}, {});

const mergeInvitations = (acc, clinicId) => ({
...acc,
[clinicId]: {
...upClinics[clinicId],
invitations: getInvitations(clinicId),
},
});
return Object.keys(upClinics).reduce(mergeInvitations, {});
}

我想做的是将两个对象合并为一个对象,我试图将诊所与邀请相关联,同时考虑到 clinicId,所以我有这个:

const upcomingClinics = {
"clinics": {
"a0CW000000271LuMAI": {
"id": "a0CW000000271LuMAI",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 1
}
},
"a0CW00000026gikMAA": {
"id": "a0CW00000026gikMAA",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 4
}
}
},
"invitations": {
"56392": {
"id": "56392",
"clinicId": "a0CW00000026gikMAA"
},
"56393": {
"id": "56393",
"clinicId": "a0CW00000026gikMAA"
},
"56402": {
"id": "56402",
"clinicId": "a0CW00000026gikMAA"
},
"56427": {
"id": "56427",
"clinicId": "a0CW000000271LuMAI"
},
"56428": {
"id": "56428",
"clinicId": "a0CW000000271LuMAI"
}
}
}

这是期望的输出,但不会引起任何突变。

const upcomingClinics = {
"clinics": {
"a0CW000000271LuMAI": {
"id": "a0CW000000271LuMAI",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 1
}
"invitations": {
"56427": {
"id": "56427",
"clinicId": "a0CW000000271LuMAI"
},
"56428": {
"id": "56428",
"clinicId": "a0CW000000271LuMAI"
}
}
},
"a0CW00000026gikMAA": {
"id": "a0CW00000026gikMAA",
"contact": {
"name": null,
"phone": null,
"email": null
},
"shifts": {
"teamLeads": 1,
"healthTechs": 4
}
"invitations": {
"56392": {
"id": "56392",
"clinicId": "a0CW00000026gikMAA"
},
"56393": {
"id": "56393",
"clinicId": "a0CW00000026gikMAA"
},
"56402": {
"id": "56402",
"clinicId": "a0CW00000026gikMAA"
},
}
}
},
}

最佳答案

您当前的尝试看起来非常接近,但我认为您有一个小错字。这:

const getInvitations = clinicId =>
Object.keys(upInvitations).reduce((acc, invId) => {
const inv = upInvitations[invId];
return inv.clinicId === clinicId ? { ...acc, [inv.clinicId]: inv } : acc;
}, {});

...正在使用 [inv.clinicId] 作为键,当 invitations 属性为您想要的 invId 时由邀请 ID 键入:

const getInvitations = clinicId =>
Object.keys(upInvitations).reduce((acc, invId) => {
const inv = upInvitations[invId];
return inv.clinicId === clinicId ? { ...acc, [invId]: inv } : acc;
}, {});

关于javascript - 将 pickBy 和 mapValues 从 lodash 转换为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51412123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com