将pickBy和mapValues从lodash转换为函数

时间:2018-07-18 23:02:11

标签: javascript object lodash reduce

我正在尝试将使用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"
        },
      }
    }
  },
}

2 个答案:

答案 0 :(得分:1)

您当前的尝试确实非常接近,但是我认为您的打字错误很小。这个:

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

...正在使用[inv.clinicId]作为键,如果您希望invId属性用邀请ID键入invitations

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

答案 1 :(得分:1)

以下操作首先使用临床编号作为键创建所有邀请的地图。

然后是所有诊所的简单循环,以将它们合并到

function clinicsWithInvitations(upcomingClinics) {

  const invitesMap = Object.entries(upcomingClinics.invitations).reduce((a, [id, invite]) => {
    const clinic = a.get(invite.clinicId)  || {}
    clinic[id] = { ...invite};
    return a.set(invite.clinicId, clinic)
  }, new Map)

  const clinics = { ...upcomingClinics.clinics};

  Object.keys(clinics).forEach(k => clinics[k].inivitations = invitesMap.get(k) || {});
  
  return {clinics: clinics};
}

console.log(clinicsWithInvitations(upcomingClinics))
.as-console-wrapper {
  max-height: 100%!important;
}
<script>
  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"
      }
    }
  }
</script>