javascript 合并2个对象数组。如果一把钥匙匹配

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 合并2个对象数组。如果一把钥匙匹配相关的知识,希望对你有一定的参考价值。

var products = [
   {
      "productId": 1,
      "cmsProductNumber": "AMAZON010",
      "pointsCost": 6000
   },
   {
      "productId": 2,
      "cmsProductNumber": "AMAZON020",
      "pointsCost": 7000
   },
   {
      "productId": 3,
      "cmsProductNumber": "AMAZON030",
      "pointsCost": 8000
   },
   {
      "productId": 4,
      "cmsProductNumber": "ITUNES010",
      "pointsCost": 6000
   },
   {
      "productId": 5,
      "cmsProductNumber": "ITUNES020",
      "pointsCost": 7000
   },
   {
      "productId": 6,
      "cmsProductNumber": "ITUNES030",
      "pointsCost": 8000
   }
]

var cardsInfo = [
   {
      "title": "iTunes gift card 10",
      "cmsProductNumber": "ITUNES010",
      "description": "Itunes is a free application for your mac or PC...",
      "termsConditionsHtml": "<div><h2>issue</h2><p>lLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut</p> </div>",
      "image": {
            "url": "https://media.radissonhotels.net/image/redeem/logo/itunes.jpg?impolicy=%s&gravity=West",
            "altText": "Mesmerizing image"
      }
   },
   {
      "title": "iTunes gift card 20",
      "cmsProductNumber": "ITUNES020",
      "description": "Itunes is a free application for your mac or PC...",
      "termsConditionsHtml": "<div><h2>issue</h2><p>lLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut</p> </div>",
      "image": {
            "url": "https://media.radissonhotels.net/image/redeem/logo/itunes.jpg?impolicy=%s&gravity=West",
            "altText": "Mesmerizing image"
      }
   },
   {
      "title": "iTunes gift card 30",
      "cmsProductNumber": "ITUNES030",
      "description": "Itunes is a free application for your mac or PC...",
      "termsConditionsHtml": "<div><h2>issue</h2><p>lLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut</p> </div>",
      "image": {
            "url": "https://media.radissonhotels.net/image/redeem/logo/itunes.jpg?impolicy=%s&gravity=West",
            "altText": "Mesmerizing image"
      }
   },
   {
      "title": "amazon gift card 10",
      "cmsProductNumber": "AMAZON010",
      "description": "amazon card...",
      "termsConditionsHtml": "<div><h2>issue</h2><p>lLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut</p> </div>",
      "image": {
            "url": "https://media.radissonhotels.net/image/redeem/logo/amazon.jpg?impolicy=%s&gravity=West",
            "altText": "Mesmerizing image"
      }
   },
   {
      "title": "amazon gift card 20",
      "cmsProductNumber": "AMAZON020",
      "description": "amazon card...",
      "termsConditionsHtml": "<div><h2>issue</h2><p>lLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut</p> </div>",
      "image": {
            "url": "https://media.radissonhotels.net/image/redeem/logo/amazon.jpg?impolicy=%s&gravity=West",
            "altText": "Mesmerizing image"
      }
   },
   {
      "title": "amazon gift card 30",
      "cmsProductNumber": "AMAZON030",
      "description": "amazon card...",
      "termsConditionsHtml": "<div><h2>issue</h2><p>lLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut</p> </div>",
      "image": {
            "url": "https://media.radissonhotels.net/image/redeem/logo/amazon.jpg?impolicy=%s&gravity=West",
            "altText": "Mesmerizing image"
      }
   }
]

var productMap = products.reduce(function(acc, product) {
   acc[product.cmsProductNumber] = product;
   return acc;
}, {});

var cardInfo = cardsInfo.map(function(cardInfo) {
	return Object.assign({}, cardInfo, productMap[cardInfo.cmsProductNumber])
});

以上是关于javascript 合并2个对象数组。如果一把钥匙匹配的主要内容,如果未能解决你的问题,请参考以下文章

提高代码效率的 20 个JavaScript 技巧和窍门

提高代码效率的 20 个JavaScript 技巧和窍门

如何比较2个长度数组的对象条目的属性并合并为一个? [复制]

合并 2 个对象数组

合并两个数组并就地修改它们(javascript)[重复]

在 JavaScript 中合并两个对象(NodeList)数组