【问题标题】:Merge Two Arrays With Child elements合并两个带有子元素的数组
【发布时间】:2021-12-16 19:57:01
【问题描述】:

我有如下两个数组,在这种情况下尝试与 id 合并,menuNo 是 id,之后,它将有一个带有子元素的新数组作为输出。带有子菜单的父菜单。

数组 1

[
  {
    "menuNo": "1001",
    "menuName": "All Forms",
    "groupNo": null,
    "groupName": null,
    "menuStatus": null,
    "groupStatus": null,
    "parentMenu": "0",
    "childMenu": null,
    "iconName": null
  },
  {
    "menuNo": "1003",
    "menuName": "All Forms 2",
    "groupNo": null,
    "groupName": null,
    "menuStatus": null,
    "groupStatus": null,
    "parentMenu": "0",
    "childMenu": null,
    "iconName": null
  },
  {
    "menuNo": "1006",
    "menuName": "All Forms 3",
    "groupNo": null,
    "groupName": null,
    "menuStatus": null,
    "groupStatus": null,
    "parentMenu": "0",
    "childMenu": null,
    "iconName": null
  }
]

数组 2

[
  {
    "menuNo": "1002",
    "menuName": "Child 1",
    "groupNo": null,
    "groupName": null,
    "menuStatus": null,
    "groupStatus": null,
    "parentMenu": "1001",
    "childMenu": null,
    "iconName": null
  },
  {
    "menuNo": "1012",
    "menuName": "Child 2",
    "groupNo": null,
    "groupName": null,
    "menuStatus": null,
    "groupStatus": null,
    "parentMenu": "1001",
    "childMenu": null,
    "iconName": null
  },
  {
    "menuNo": "1014",
    "menuName": "Child 1",
    "groupNo": null,
    "groupName": null,
    "menuStatus": null,
    "groupStatus": null,
    "parentMenu": "1006",
    "childMenu": null,
    "iconName": null
  },
  {
    "menuNo": "1019",
    "menuName": "Child 2",
    "groupNo": null,
    "groupName": null,
    "menuStatus": null,
    "groupStatus": null,
    "parentMenu": "1006",
    "childMenu": null,
    "iconName": null
  },

json 中的预期结果

[{
  "menuNo": "1001",
  "menuName": "All Forms",
  "groupNo": null,
  "groupName": null,
  "menuStatus": null,
  "groupStatus": null,
  "parentMenu": "0",
  "childMenu": "Child 1", "Child 2",
  "iconName": null
},
{
  "menuNo": "1006",
  "menuName": "All Forms 3",
  "groupNo": null,
  "groupName": null,
  "menuStatus": null,
  "groupStatus": null,
  "parentMenu": "0",
  "childMenu": "Child 1", "Child 2"
  "iconName": null
}]

试过了,但它只合并了 id 上的所有数据,如下所示:

//Merge two jSon object
const mergeById = (array1, array2) =>
    array1.map(itm => ({
      ...array2.find((item) => (item.id === itm.id) && item),
      ...itm
}));

我的计划是创建带有子菜单的菜单作为列表。我该怎么做?

【问题讨论】:

    标签: arrays angular typescript angular8


    【解决方案1】:
    1. 您需要在child.parentMenu === parent.menuNo 的基础上将Array.filter 用于array2
    2. 接下来,过滤器child 不是空数组,也不是null
    const mergeById = (array1, array2) =>
    array1.map(parent => ({
      ...parent,
      childMenu: array2.filter((child) => child && (child.parentMenu === parent.menuNo))
        .map(child => child && child.menuName)
    }));
    
    var result = mergeById(array1, array2)
      .filter(x => x.childMenu && x.childMenu.length > 0);
    

    代码 sn-p & 结果

    var array1 = [
    {
      "menuNo": "1001",
      "menuName": "All Forms",
      "groupNo": null,
      "groupName": null,
      "menuStatus": null,
      "groupStatus": null,
      "parentMenu": "0",
      "childMenu": null,
      "iconName": null
    },
    {
      "menuNo": "1003",
      "menuName": "All Forms 2",
      "groupNo": null,
      "groupName": null,
      "menuStatus": null,
      "groupStatus": null,
      "parentMenu": "0",
      "childMenu": null,
      "iconName": null
    },
    {
      "menuNo": "1006",
      "menuName": "All Forms 3",
      "groupNo": null,
      "groupName": null,
      "menuStatus": null,
      "groupStatus": null,
      "parentMenu": "0",
      "childMenu": null,
      "iconName": null
    }
    ];
    
    var array2 = [
    {
      "menuNo": "1002",
      "menuName": "Child 1",
      "groupNo": null,
      "groupName": null,
      "menuStatus": null,
      "groupStatus": null,
      "parentMenu": "1001",
      "childMenu": null,
      "iconName": null
    },
    {
      "menuNo": "1012",
      "menuName": "Child 2",
      "groupNo": null,
      "groupName": null,
      "menuStatus": null,
      "groupStatus": null,
      "parentMenu": "1001",
      "childMenu": null,
      "iconName": null
    },
    {
      "menuNo": "1014",
      "menuName": "Child 1",
      "groupNo": null,
      "groupName": null,
      "menuStatus": null,
      "groupStatus": null,
      "parentMenu": "1006",
      "childMenu": null,
      "iconName": null
    },
    {
      "menuNo": "1019",
      "menuName": "Child 2",
      "groupNo": null,
      "groupName": null,
      "menuStatus": null,
      "groupStatus": null,
      "parentMenu": "1006",
      "childMenu": null,
      "iconName": null
    }
    ];
    
    const mergeById = (array1, array2) =>
    array1.map(parent => ({
      ...parent,
      childMenu: array2.filter((child) => child && (child.parentMenu === parent.menuNo))
        .map(child => child && child.menuName)     
    }));
    
    var result = mergeById(array1, array2)
      .filter(x => x.childMenu && x.childMenu.length > 0);
    
    console.log(result);

    【讨论】:

    • 在某些情况下,child 可以为空。我想,以上应该可以工作。
    • 更新为array2.filter((child) => child && (child.parentMenu === parent.menuNo))
    • 你能检查一下帖子中的预期输出吗?更新了,可能是我说的不够清楚。是的,完全正确。
    • 您的 JSON 无效,childMenu 必须是 "childMenu": ["Child 1", "Child 2"]
    • 已解决@Yong Shun - 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 2018-01-04
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多