简体   繁体   English

Mongodb 组合两个对象

[英]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.我有此代码返回 mongodb 查询,该查询从购物清单中查询购物车,我已将其分配在名为“obj”的 object 中,并在控制台中打印它。

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:我有这个 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'
  }
}

How can I combine two objects that will result into these?如何将两个对象组合成这些对象? obj and req.session.cart obj 和 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'
  }
}

You can iterate req.session.cart and insert into obj session cart this way:您可以这样迭代req.session.cart并插入到obj session 购物车中:

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);

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. Object.assign() 方法将所有可枚举的自身属性从一个或多个源对象复制到目标 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM