sonjasan
sonjasan

Reputation: 49

Mongodb combining two objects

I have this code that returns mongodb query that queries a cart from grocery list, I have assigned it in an object named "obj" the print it in the console.

const cart = await db.grocerylst.findOne({ userId: req.session.custid });
var obj = {};
obj = cart.cart;
console.log(obj);

and the result of this query is :

{
  '11f3eba8c2168a485ceba687d7c07571bb7633b8': {
    productId: 5e2a85b6d052e12078d40418,
    title: 'JOHNNIE WALKER® PLATINUM LABEL™ 18 YEAR OLD',
    quantity: 10,
    totalItemPrice: 37980,
    options: {},
    productImage: '/uploads/5e2a85b6d052e12078d40418/01-johnnie-walker-platinum-18-year-old.png',
    productComment: null,
    productSubscription: null,
    productFrom: {
      _id: '5e2a4e78d38f491d0082b949',
      storeName: 'Tiongsan Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Johnywalkerplatinum'
  },
  '204dd16ce5682cc517423ec56d086ef8f56d09b0': {
    productId: 5e5e0e371c03c01af4ae06de,
    title: 'Nutella 3kg Bucket',
    quantity: 2,
    totalItemPrice: 3100,
    options: {},
    productImage: '/uploads/5e5e0e371c03c01af4ae06de/1546613761-imageService.jpg',
    productComment: null,
    productSubscription: null,
    productFrom: {
      _id: '5e2a4e78d38f491d0082b949',
      storeName: 'Tiongsan Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Nutella_3kg_Bucket'
  }
}

and I have this req.session.cart that has a value of :

{
  '770d832f5d33e5381790a0393388ac150148bfd6': {
    productId: 5e5e0d991c03c01af4ae06dd,
    title: 'Nutella Hazelnut Spread with Cocoa 900g',
    quantity: 1,
    totalItemPrice: 455,
    options: {},
    productImage: '/uploads/5e5e0d991c03c01af4ae06dd/nutella.jpg',
    productComment: null,
    productSubscription: undefined,
    productFrom: {
      _id: '5e2a639744360f00dca93a9d',
      storeName: 'SM Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Nutella_Hazelnut_Spread_with_Cocoa_900g'
  }
}

How can I combine two objects that will result into these? obj and req.session.cart

{
  '11f3eba8c2168a485ceba687d7c07571bb7633b8': {
    productId: 5e2a85b6d052e12078d40418,
    title: 'JOHNNIE WALKER® PLATINUM LABEL™ 18 YEAR OLD',
    quantity: 10,
    totalItemPrice: 37980,
    options: {},
    productImage: '/uploads/5e2a85b6d052e12078d40418/01-johnnie-walker-platinum-18-year-old.png',
    productComment: null,
    productSubscription: null,
    productFrom: {
      _id: '5e2a4e78d38f491d0082b949',
      storeName: 'Tiongsan Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Johnywalkerplatinum'
  },
  '204dd16ce5682cc517423ec56d086ef8f56d09b0': {
    productId: 5e5e0e371c03c01af4ae06de,
    title: 'Nutella 3kg Bucket',
    quantity: 2,
    totalItemPrice: 3100,
    options: {},
    productImage: '/uploads/5e5e0e371c03c01af4ae06de/1546613761-imageService.jpg',
    productComment: null,
    productSubscription: null,
    productFrom: {
      _id: '5e2a4e78d38f491d0082b949',
      storeName: 'Tiongsan Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Nutella_3kg_Bucket'
  },
  '770d832f5d33e5381790a0393388ac150148bfd6': {
    productId: 5e5e0d991c03c01af4ae06dd,
    title: 'Nutella Hazelnut Spread with Cocoa 900g',
    quantity: 1,
    totalItemPrice: 455,
    options: {},
    productImage: '/uploads/5e5e0d991c03c01af4ae06dd/nutella.jpg',
    productComment: null,
    productSubscription: undefined,
    productFrom: {
      _id: '5e2a639744360f00dca93a9d',
      storeName: 'SM Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Nutella_Hazelnut_Spread_with_Cocoa_900g'
  }
}

Upvotes: 1

Views: 49

Answers (2)

sonjasan
sonjasan

Reputation: 49

Object.assign(req.session.cart, cart.cart);

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object.

Upvotes: 0

Valijon
Valijon

Reputation: 13103

You can iterate req.session.cart and insert into obj session cart this way:

for (var key in req.session.cart) {
    obj[key] = req.session.cart[key];
}

DEMO

var obj = {
  '11f3eba8c2168a485ceba687d7c07571bb7633b8': {
    productId: '5e2a85b6d052e12078d40418',
    title: 'JOHNNIE WALKER® PLATINUM LABEL™ 18 YEAR OLD',
    quantity: 10,
    totalItemPrice: 37980,
    options: {},
    productImage: '/uploads/5e2a85b6d052e12078d40418/01-johnnie-walker-platinum-18-year-old.png',
    productComment: null,
    productSubscription: null,
    productFrom: {
      _id: '5e2a4e78d38f491d0082b949',
      storeName: 'Tiongsan Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Johnywalkerplatinum'
  },
  '204dd16ce5682cc517423ec56d086ef8f56d09b0': {
    productId: '5e5e0e371c03c01af4ae06de',
    title: 'Nutella 3kg Bucket',
    quantity: 2,
    totalItemPrice: 3100,
    options: {},
    productImage: '/uploads/5e5e0e371c03c01af4ae06de/1546613761-imageService.jpg',
    productComment: null,
    productSubscription: null,
    productFrom: {
      _id: '5e2a4e78d38f491d0082b949',
      storeName: 'Tiongsan Supermarket',
      storeLocation: 'Baguio City'
    },
    link: 'Nutella_3kg_Bucket'
  }
};
var req = { 
   session: { 
     cart: {
       '770d832f5d33e5381790a0393388ac150148bfd6': {
       productId: '5e5e0d991c03c01af4ae06dd',
       title: 'Nutella Hazelnut Spread with Cocoa 900g',
       quantity: 1,
       totalItemPrice: 455,
       options: {},
       productImage: '/uploads/5e5e0d991c03c01af4ae06dd/nutella.jpg',
       productComment: null,
       productSubscription: undefined,
       productFrom: {
        _id: '5e2a639744360f00dca93a9d',
        storeName: 'SM Supermarket',
        storeLocation: 'Baguio City'
      },
      link: 'Nutella_Hazelnut_Spread_with_Cocoa_900g'
      }
    }
  }
};

for (var key in req.session.cart) {
    obj[key] = req.session.cart[key];
}

//Remove this code
document.body.appendChild(document.createElement('pre')).innerHTML = JSON.stringify(obj, undefined, 4);

Upvotes: 1

Related Questions