Home > Blockchain >  Having some issue when trying to group objects from nested array of objects
Having some issue when trying to group objects from nested array of objects

Time:03-14

I am trying to group some nested array of objects. On my below data, there is a 'products' key property. I am trying to group the products if the vendor.email is the same. After grouping the products I also want to put the paymentDetails key property on each group.

This is my code. I am able to group the products but am not able to set the paymentDetails key and value on each group.

const data= [
        {
            _id: "622d70a49bd88b1599026318",
            products: [
                {
                    _id: "6223186e2278d4e502f5264a",
                    title: "Product number 1",
                    price: 600,
                    cartQuantity: 1,
                    vendor: {email: "[email protected]"}
                },
                {
                    _id: "622d4e9f9bd88b1599026317",
                    title: "asdas",
                    price: 100,
                    cartQuantity: 5,
                    vendor: {
                        email: "[email protected]"
                    }
                },
                 {
                    _id: "622d4e9f9bd88b1599026317",
                    title: "asdas",
                    price: 100,
                    cartQuantity: 5,
                    vendor: {
                        email: "[email protected]"
                    }
                },
            ],
            paymentDetails: {
                createdId: 1647145079,
                date: "Sun Mar 13 2022",
                amount: 700,
                email: "[email protected]",
                last4: "4242",
                transaction: "p"
            },
            status: "Pending",
            billing: {
                country: "BD",
                name: "Md. Fathe Karim",
                phone: " 88010000000",
                line1: "Madhabdi",
                city: "Narshingdi",
                postal_code: "1604",
                state: "Bandarban"
            }
        }]

const mapped = {};                       // MY function
data[0].products.forEach(item => {
  if (item.vendor.email in mapped) return mapped[item.vendor.email].push(item); 
  mapped[item.vendor.email] = [item];
});

const expectedFormat = Object.keys(mapped).map(key => {
  const o = {}; 
  o["orders"] = mapped[key];
  return o; 
});
console.log(expectedFormat)

My expected result is:

[
  {
    "orders": [
      {
        "_id": "6223186e2278d4e502f5264a",
        "title": "Product number 1",
        "price": 600,
        "vendor": {
          "email": "[email protected]"
        }
      }
    ],
    "paymentDetails": {
      createdId: 1647145079,
      amount: 700,
      email: "[email protected]",
    }
  },
  {
    "orders": [
      {
        "_id": "622d4e9f9bd88b1599026317",
        "title": "Product number 2",
        "price": 100,
        "vendor": {
          "email": "[email protected]"
        }
      },
      {
        "_id": "622d4e9f9bd88b1599026317",
        "title": "Product number 3",
        "price": 100,
        "vendor": {
          "email": "[email protected]"
        }
      }
    ],
    "paymentDetails": {
      createdId: 1647145079,
      amount: 700,
      email: "[email protected]",
    }
  }
]

CodePudding user response:

Does expectedFormat.forEach(orders => orders.paymentDetails = data[0].paymentDetails); work? I might be misunderstanding.

  • Related