Home > Software design >  How to group nested array of objects in js
How to group nested array of objects in js

Time:03-14

I have a nested array of objects. I'm trying to group product objects that have the same value. The below object has a vendor key also there is an email inside it. So I am trying to group the objects if the product vendor email is the same.

This is my database how it looks like `

[
        {
            _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]"
                    }
                }
            ]
        }]

I am trying to do it with the reduce method but the problem is for using the map method inside the reduce it Repeat the object many times. Also did not able to get the group of the same objects.

const groupedMap = db.reduce(
    (entryMap, e) => e.products.map((product) => entryMap.set(product.vendor.email, [...entryMap.get(product)||[], product])),
    new Map()
);

The above code output is: enter image description here

My expectation is:

[0: {"[email protected]" => Array(1)}
   key: "[email protected]"
   value: [{_id: '6223186e2278d4e502f5264a', title: 'Product number 1', price: 600, cartQuantity: 1, vendor: {email: "[email protected]"}}],
1: {"[email protected]" => Array(2)}
key: "[email protected]"
   value: [{_id: '6223186e2278d4e502f5264a', title: 'Product number 1', price: 600, cartQuantity: 1, vendor: {email: "[email protected]"}},
{_id: '6223186e2278d4e502f5264a', title: 'Product number 1', price: 600, cartQuantity: 1, vendor: {email:"[email protected]"}}
]
]

CodePudding user response:

try to make an object where key is the value you group by. the next step is foreach in which you check each object in the array, whether the value you are looking for is in the object made - if not, then you filter the array by searched value and add the result to your object

CodePudding user response:

Loop through each item in the array and see if the vendor email exists as a key already in a dictionary, if it exists push it to that array, otherwise set the value of the vendor email key equal to an array with the current item in it

See code below

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]"
      }
    }
  ]
}];

const mapped = {};
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[key] = mapped[key];
  
  return o;
});

console.log(expectedFormat)

  • Related