合并分组的 2 个对象数组并在 JavaScript 上排除重复项

我有 2 个具有父数组和子数组的分组数组,我想合并这两个具有相同父数组的数组,但我仅复制具有唯一顺序的子数组,我在互联网上搜索了许多文章,但尚未找到解决方案。请帮我。这是我的代码


var groupOrder = [

  {

    group_time_str: "25 Apr 2019",

    orders: [

      {

        vertical_id: "1",

        vertical_category: "A"

      },

      {

        vertical_id: "2",

        vertical_category: "B"

      }

    ]

  },

  {

    group_time_str: "26 Apr 2019",

    orders: [

      {

        vertical_id: "1",

        vertical_category: "A"

      },

      {

        vertical_id: "2",

        vertical_category: "B"

      },

      {

        vertical_id: "3",

        vertical_category: "C"

      }

    ]

  }

];

var groupOrder2 = [

  {

    group_time_str: "26 Apr 2019",

    orders: [

      {

        vertical_id: "3",

        vertical_category: "C"

      },

      {

        vertical_id: "4",

        vertical_category: "D"

      }

    ]

  },

  {

    group_time_str: "27 Apr 2019",

    orders: [

      {

        vertical_id: "1",

        vertical_category: "A"

      },

      {

        vertical_id: "2",

        vertical_category: "B"

      }

    ]

  }

];

const combine = [...groupOrder, ...groupOrder2];

const groupBy = combine.reduce((acc, cur) => {

  acc[cur.group_time_str]

    ? (acc[cur.group_time_str].orders = [

        ...acc[cur.group_time_str].orders,

        ...cur.orders

      ])

    : (acc[cur.group_time_str] = cur);

  return acc;

}, {});

console.log(groupBy);

body {

  font-family: sans-serif;

}

<h1>Grouped Order</h1>

您会看到2019 年 4 月 26 日组中有重复的


{

   vertical_id: "3",

   vertical_category: "C"

},

我怎样才能删除重复的?请帮助我,谢谢


慕沐林林
浏览 128回答 2
2回答

尚方宝剑之说

尝试减少第一个数组以仅获取不匹配的项目,而不是在开头合并数组:const groupOrder = [{group_time_str: "25 Apr 2019",orders: [{vertical_id: "1",vertical_category: "A"},{vertical_id: "2",vertical_category: "B"}]},{group_time_str: "26 Apr 2019",orders: [{vertical_id: "1",vertical_category: "A"},{vertical_id: "2",vertical_category: "B"},{vertical_id: "3",vertical_category: "C"}]}];const groupOrder2 = [{group_time_str: "26 Apr 2019",orders: [{vertical_id: "3",vertical_category: "C"},{vertical_id: "4",vertical_category: "D"}]},{group_time_str: "27 Apr 2019", orders: [{vertical_id: "1",vertical_category: "A"},{vertical_id: "2",vertical_category: "B"}]}];const result = groupOrder.reduce((arr, g1) => {&nbsp; const g2match = groupOrder2.find(&nbsp; &nbsp; g2 => g2.group_time_str === g1.group_time_str&nbsp; );&nbsp; if (g2match) {&nbsp; &nbsp; const orders = g1.orders.filter(&nbsp; &nbsp; &nbsp; g1o =>&nbsp; &nbsp; &nbsp; &nbsp; !g2match.orders.find(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g1o.vertical_id === m.vertical_id &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g1o.vertical_category === m.vertical_category&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp; &nbsp; g2match.orders = [...g2match.orders, ...orders];&nbsp; &nbsp; return arr;&nbsp; }&nbsp; return [...arr, { ...g1 }];}, []);console.log([...result, ...groupOrder2]);

烙印99

您可以执行以下操作,过滤掉 cur.orders 以删除重复项,var groupOrder = [&nbsp; {&nbsp; &nbsp; group_time_str: "25 Apr 2019",&nbsp; &nbsp; orders: [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "1",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "A"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "2",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "B"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; group_time_str: "26 Apr 2019",&nbsp; &nbsp; orders: [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "1",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "A"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "2",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "B"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "3",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "C"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; }];var groupOrder2 = [&nbsp; {&nbsp; &nbsp; group_time_str: "26 Apr 2019",&nbsp; &nbsp; orders: [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "3",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "C"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "4",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "D"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; },&nbsp; {&nbsp; &nbsp; group_time_str: "27 Apr 2019",&nbsp; &nbsp; orders: [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "1",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "A"&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; vertical_id: "2",&nbsp; &nbsp; &nbsp; &nbsp; vertical_category: "B"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; }];const combine = [...groupOrder, ...groupOrder2];const groupBy = combine.reduce((acc, cur) => {&nbsp; acc[cur.group_time_str]&nbsp; &nbsp; ? (acc[cur.group_time_str].orders = [&nbsp; &nbsp; &nbsp; &nbsp; ...acc[cur.group_time_str].orders,&nbsp; &nbsp; &nbsp; &nbsp; ...cur.orders.filter(item => acc[cur.group_time_str].orders.findIndex(accOrder => item['vertical_id'] === accOrder['vertical_id'] && item['vertical_category'] === accOrder['vertical_category']) <= -1)&nbsp; &nbsp; &nbsp; ])&nbsp; &nbsp; : (acc[cur.group_time_str] = cur);&nbsp; return acc;}, {});console.log(groupBy);body {&nbsp; font-family: sans-serif;}<h1>Grouped Order</h1>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript