尝试按项目编号连接两个JSON数组

时间:2016-12-28 15:24:33

标签: jquery arrays json

我有两个JSON文件。一个包括所有产品数据,另一个包括营养数据。目前,我只是连接两个数组,但我需要组合这两个数组中的对象。

在产品JSON文件中,我有“itemNum”,在营养JSON文件中我有“itemFullUPC”。这两个数据库中每种产品的数量相同。因此,对于两个数据库中的一个特定产品,“itemFullUPC和”itemNum“都等于”070796400087“。

如何合并这两个数据库以及匹配“itemNum”和“itemFullUPC”的对象中的所有内容?希望这是有道理的。

$.ajax({
    dataType: "jsonp",
    url: 'http://50.73.209.125:8080/api/ItemMaster/',
    success: function(json){
    //assign JSON to product data variable
    product_data = json;

  $.ajax({
    dataType: "jsonp",
    url: 'http://50.73.209.125:8080/api/ItemNutrition/',
    success: function(json){
    //assign JSON to nutrition data variable
    nutritional_data = json;


    var json = product_data.concat(nutritional_data);
    console.log(json);

        }
     });  
   }
});

营养数据库对象示例

{
  "_id":"5791193f8d30bc8e78002ced",
  "itemLastUpdated":"12/9/2016 1:02:31 PM",
  "itemSelenium":0,
  "itemZinc":0,
  "itemMagnesium":0,
  "itemIodine":0,
  "itemPhosphorus":0,
  "itemPanthoAcid":0,
  "itemBiotin":0,
  "itemVitaminB12":0,
  "itemFolate":0,
  "itemVitaminB6":0,
  "itemManga":0,
  "itemNiacin":0,
  "itemRiboflavin":0,
  "itemThiamin":0,
  "itemVitaminK":0,
  "itemVitaminE":0,
  "itemCopper":0,
  "itemVitaminD":0,
  "itemIron":0,
  "itemCalcium":0,
  "itemVitaminC":0,
  "itemVitaminA":0,
  "itemPolyUnsatFat":0,
  "itemSaturFat":0,
  "itemTotalFat":0,
  "itemSugars":0,
  "itemDietFiber":0,
  "itemTotalCarb":1,
  "itemPotassium":0,
  "itemSodium":500,
  "itemCholesterol":0,
  "itemMonoUnsatFat":0,
  "itemTransFat":0,
  "itemProtein":1,
  "itemSugarAlcohol":0,
  "itemCaloriesFromFat":0,
  "itemCalories":5,
  "itemServings":6,
  "itemIngredients":"Chicken Broth, Contains less than 1% of the following: Salt, Dextrose, Monosodium Glutamate, Maltodextrin, Flavor",
  "servingSize":"1 cup",
  "servingSizeUnit":"cup",
  "servingSizeQnty":1,
  "itemNum":"070796400087",
  "__v":0
}

产品数据库对象示例

{
  "_id":"577411f7cce3c4c741000001",
  "itemGMOFree":"N",
  "itemBrandLetter":"C",
  "itemKosherSym":"N",
  "itemShipper":"N",
  "itemRefridge":"N",
  "itemFrozen":"N",
  "itemPreWeight":"Y",
  "itemDeli":"N",
  "itemGlutenFree":"Y",
  "itemHoliday":"N",
  "itemSeasonBuy":"Y",
  "itemScannable":"Y",
  "itemUnlabeled":"N",
  "itemPalletWeight":2500,
  "itemPalletTiers":10,
  "itemPalletBlocks":10,
  "itemCaseCube":0.56,
  "itemCaseDepth":16.25,
  "itemCaseWidth":12,
  "itemCaseHeight":5,
  "itemCaseWeight":24.5,
  "itemCaseUnits":12,
  "itemPieceCube":0.043,
  "itemPieceDepth":4,
  "itemPieceWidth":4,
  "itemPieceHeight":4.65,
  "itemPieceWeight":1.75,
  "itemNetContent":"28.00",
  "itemFullUPC":"070796400087",
  "itemCountry":"Italy",
  "itemPackSize":"12/28 oz",
  "itemUOM":"OZ",
  "itemDescription":"Whole peeled San Marzano plum tomatoes are specially grown at the base of Mt. Vesuvius, and are freshly packed with basil in puree. This type of tomato is sweeter, less acidic, contains less seeds, and has higher pectin than other tomatoes.",
  "itemName":"Cento San Marzano Certified Tomatoes",
  "itemBuildNum":0,
  "itemSuffix":7,
  "itemItem":40008,
  "itemMFG":70796,
  "itemPrefix":0,
  "itemCase_GTIN":30,
  "itemPiece_GTIN":0,
  "imageURL":"http://centogallery.zenfolio.com/img/s5/v130/p961607729-3.jpg",
  "itemCommodity":"1120",
  "__v":0,
  "itemLastUpdated":"12/21/2016 1:06:28 PM",
  "itemVendor":17477,
  "itemBPAFree":"",
  "itemCategory":1,
  "itemDairy":"",
  "itemEgg":"",
  "itemLowSodium":"",
  "itemOrganic":"",
  "itemPeanuts":"",
  "itemShellfish":"",
  "itemSoy":"",
  "itemTreeNuts":"",
  "itemWheat":"",
  "itemWholeGrain":""
}

注意这两个对象如何都包含相同的项目编号“070796400087”。我需要结合这两个例子,其中itemNum匹配itemsFullUPC。

1 个答案:

答案 0 :(得分:1)

这可以通过将array2对象分配为属性来将它们链接到新的JS对象。阅读javascript关联数组以便更好地理解。

    // Assuming 2 arrays array1 & array2 - using dummy data here for clarity. Important to note that both have some matching value.

    var array1 = JSON.parse('[{"itemNum": "1234a", "otherParam": "A1"},{"itemNum": "5678a", "otherParam": "B2"}]')

    var array2 = JSON.parse('[{"itemFullUPC": "1234a", "itemDescription": "Details about product 1234a"},{"itemFullUPC": "5678a", "itemDescription": "Details about product 5678a"}]')

   
    // Load phase - run any time after array 2 is loaded
    var lookup = {}
    for (var i = 0; i < array2.length; i = i + 1){
        obj = array2[i];
        lookup[obj.itemFullUPC] = obj; // creates a property in lookup object with key of itemFullPC
    }
        
    function getProdDetails(idx){
        var prod = array1[idx]
        console.log("itemDescription for " + prod.itemNum + " = " + lookup[prod.itemNum].itemDescription)
    }

    getProdDetails(1)
    getProdDetails(0)