Merge Grouped 2 Array of Object Exclude Duplicate Item on JavaScript

Viewed 67

i have 2 grouped array that have parent and child array, i want to merge these 2 array that have same parent, but the child that i copy only that have unique order, i search for many articles in internet but not yet found the solution. please help me. there is my code

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>

you see that on group 26 Apr 2019 there is duplicate

{
   vertical_id: "3",
   vertical_category: "C"
},

how i can remove the duplicate one? please help me, thank you

2 Answers

Try to reduce the first array to get only the items that don't match instead of merging the arrays at the beginning:

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) => {
  const g2match = groupOrder2.find(
    g2 => g2.group_time_str === g1.group_time_str
  );
  if (g2match) {
    const orders = g1.orders.filter(
      g1o =>
        !g2match.orders.find(
          m =>
            g1o.vertical_id === m.vertical_id &&
            g1o.vertical_category === m.vertical_category
        )
    );
    g2match.orders = [...g2match.orders, ...orders];
    return arr;
  }
  return [...arr, { ...g1 }];
}, []);

console.log([...result, ...groupOrder2]);

You can do the following filtering out the cur.orders to remove duplicates,

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.filter(item => acc[cur.group_time_str].orders.findIndex(accOrder => item['vertical_id'] === accOrder['vertical_id'] && item['vertical_category'] === accOrder['vertical_category']) <= -1)
      ])
    : (acc[cur.group_time_str] = cur);
  return acc;
}, {});
console.log(groupBy);
body {
  font-family: sans-serif;
}
<h1>Grouped Order</h1>

Related