2016-12-28 32 views
1

我有兩個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); 

     } 
    }); 
    } 
}); 

營養DB對象的實例

{ 
    "_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 
} 

產品DB對象的實例

{ 
    "_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匹配的這兩個示例結合起來。

+0

你能後一對夫婦的每個JSON的例子構建? –

+0

@VanquishedWombat我把它放在ty – Tom

+0

我想你想合併,這樣當用戶從數組1中選擇一個產品時,你不需要運行數組2的大循環 - 對嗎? –

回答

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)