簡體   English   中英

Mongodb 組合兩個對象

[英]Mongodb combining two objects

我有此代碼返回 mongodb 查詢,該查詢從購物清單中查詢購物車,我已將其分配在名為“obj”的 object 中,並在控制台中打印它。

const cart = await db.grocerylst.findOne({ userId: req.session.custid });
var obj = {};
obj = cart.cart;
console.log(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'
  }
}

我有這個 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'
  }
}

如何將兩個對象組合成這些對象? 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'
  }
}

您可以這樣迭代req.session.cart並插入到obj session 購物車中:

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

演示

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

Object.assign() 方法將所有可枚舉的自身屬性從一個或多個源對象復制到目標 object。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM