javascript - 如何使用父对象中对每个子对象的 ID 引用将主对象与子对象合并

标签 javascript ecmascript-6 javascript-objects

我有一个主要父集合(项目)和 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))
}));

关于javascript - 如何使用父对象中对每个子对象的 ID 引用将主对象与子对象合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60157788/

相关文章:

javascript - Ember 计算未更新

Javascript 代码不适用于 IE11,但适用于所有其他浏览器

javascript - Angularjs $apply() 用于 $rootscope 变量?

javascript - 使用 jQuery.load();在使用 href 作为参数时仅加载文档的一部分

javascript - 为数组中的每个键执行函数 w/o 循环

javascript - onEndReached 导致 ListView 上显示多个项目

javascript - p5.j​​s && ecmascript6 表示法

javascript - 迭代生成器的更简单方法?

javascript - 获取具有默认值的深度嵌套值的最佳方法

JavaScript,检查嵌套对象属性是否为 null/undefined 的优雅方式