[英]How to merge Primary object with child objects using and ID reference in Parent to each child
我有一个主要的父集合(项目)和 2 个存储为对象数组的子集合。
const projects = {
items: [
{
name: "Logo and Branding for Iron Kettle",
skills: [
"579b150756ea2dba79c1f09f",
"579b150756ea2dba79c1f062"
],
solutions: [
"579b150756ea2dba79c1f09e",
"579b150756ea2dba79c1f077"
],
_cid: "579b150756ea2dba79c1efcb",
_id: "579b150756ea2dba79c1f0af"
},
{
name: "Snow shoot for media company",
skills: [
"579b150756ea2dba79c1f072",
"579b150756ea2dba79c1f062"
],
solutions": [
"579b150756ea2dba79c1f09e",
"579b150756ea2dba79c1f071",
],
_cid: "579b150756ea2dba79c1efcb",
_id: "579b150756ea2dba79c1f06a"
}
],
count: 12,
limit: 100,
offset: 0,
total: 12
};
const skills = {
items: [
{
name: "Logo Design",
_cid: "579b150756ea2dba79c1efdf",
_id: "579b150756ea2dba79c1f09f"
},
{
name: "Brand Research",
_cid: "579b150756ea2dba79c1efdf",
_id: "579b150756ea2dba79c1f072"
},
{
name: "Graphic Design",
_cid: "579b150756ea2dba79c1efdf",
_id: "579b150756ea2dba79c1f062"
}
],
count: 13,
limit: 100,
offset: 0,
total: 13
};
const solutions = {
items: [
{
name: "Location research",
_cid: "579b150756ea2dba79c1eff3",
_id: "579b150756ea2dba79c1f09e"
},
{
name: "Complete photo shoot",
_cid: "579b150756ea2dba79c1eff3",
_id: "579b150756ea2dba79c1f071"
},
{
name: "Competition Analysis",
_cid: "579b150756ea2dba79c1eff3",
_id: "579b150756ea2dba79c1f077"
}
],
count: 19,
limit: 100,
offset: 0,
total: 19
};
我需要返回一个新的项目数组,其中的技能和解决方案 ID 填充了相关数组中的实际对象。
这将是新数组中一项的最终结果。
{
name: "Logo and Branding for Iron Kettle",
skills: [
{
name: "Logo Design",
_cid: "579b150756ea2dba79c1efdf",
_id: "579b150756ea2dba79c1f09f"
},
{
name: "Graphic Design",
_cid: "579b150756ea2dba79c1efdf",
_id: "579b150756ea2dba79c1f062"
}
],
solutions: [
{
name: "Location research",
_cid: "579b150756ea2dba79c1eff3",
_id: "579b150756ea2dba79c1f09e"
},
{
name: "Competition Analysis",
_cid: "579b150756ea2dba79c1eff3",
_id: "579b150756ea2dba79c1f077"
}
],
_cid: "579b150756ea2dba79c1efcb",
_id: "579b150756ea2dba79c1f0af"
},
尝试这样的事情:
projects.items.map(i => ({
...i,
skills: i.skills.map(s => skills.items.find(skill => skill._id === s)),
solutions: i.solutions.map(s => solutions.items.find(solution => solution._id === s))
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.