How to merge array of objects element in javascript using common property

const data = [ { date: "2021-05-17", meals: [{ name: "jon", breakFast: 1, lunch: 2, … Read more How to merge array of objects element in javascript using common property

     const data = [
  {
    date: "2021-05-17",
    meals: [{ name: "jon", breakFast: 1, lunch: 2, dinner: 2 }],
  },
  {
    date: "2021-05-17",
    meals: [{ name: "Bob", breakFast: 3, lunch: 4, dinner: 5 }],
  },
  {
    date: "2021-05-18",
    meals: [{ name: "Boo", breakFast: 3, lunch: 4, dinner: 5 }],
  },
];

I have an array of objects. There has different date and meals(array of objects). Is it possible to show all same date meals together?

Like this one.

  const data = [
  {
    date: "2021-05-17",
    meals: [
      { name: "jon", breakFast: 1, lunch: 2, dinner: 2 },
      { name: "Bob", breakFast: 3, lunch: 4, dinner: 5 },
    ],
  },
  {
    date: "2021-05-18",
    meals: [{ name: "Boo", breakFast: 3, lunch: 4, dinner: 5 }],
  },
];

Source: JavaSript – Stack Overflow


Categories: NewsTags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *