2016-11-10 2 views
-1

Ich habe zwei JSON-Dateien:JSON Wert und die Geschwister Eigenschaftswert anzuzeigen

Erste JSON:

$scope.deviceList = 
     { 
    "devices": [ 
    { 
     "sub_family": "6s Plus", 
     "presales_description": "iPhone 6 Plus features a stunning 5.5-inch Retina HD display, amazing cameras and many advanced features, all in a breakthrough design. A 5.5-inch Retina HD display. An A8 chip with 64-bit desktop-class architecture. 8MP iSight camera with Focus Pixels and optical image stabilisation. Touch ID. Fast 4G LTE and Wi-Fi.1 Long battery life.2 And iOS 9 and iCloud.", 
     "Camera": true, 
     "device_type": "smartphone", 
     "presales_vendor": "Apple", 
     "sim_form-factor": "Nano", 
     "memory": 64, 
     "device_class": "HC14", 
     "TV Capable": true, 
     "family": "iPhone", 
     "specification_reference": "D20", 
     "variants": [ 
     { 
      "colour": "Space Grey", 
      "default": true, 
      "presales_sequence": 961, 
      "s_code": "S0346715", 
      "credit_risk": 750, 
      "colour_code": "#898989", 
      "max_credit_term": 24, 
      "list_price": 759, 
      "presales_start_date": "02/04/2016" 
     }, 
     { 
      "colour": "Silver", 
      "default": true, 
      "presales_sequence": 961, 
      "s_code": "S0346717", 
      "credit_risk": 750, 
      "colour_code": "#e5e5e5", 
      "max_credit_term": 24, 
      "list_price": 759, 
      "presales_start_date": "02/04/2016" 
     } 
    ] 
    } 
] 

}

Zweite JSON:

{ 
    "stocks":[ 
     { 
     "estAvailableDate":"", 
     "quantityAvailable":"0", 
     "obsolete":"false", 
     "handsetScode":"S0346717", 
     "backOrderable":"Y", 
     "stockState":"OutOfStock", 
     "stock_update":"13/10/2016 10:57", 
     "preorder":true, 
     "stock_next_amount":"0" 
     }, 
     { 
     "estAvailableDate":"", 
     "quantityAvailable":"0", 
     "obsolete":"false", 
     "handsetScode":"S0346715", 
     "backOrderable":"Y", 
     "stockState":"OutOfStock", 
     "stock_update":"13/10/2016 10:57", 
     "preorder":true, 
     "stock_next_amount":"0" 
     } 
    ] 
    } 

Ich muss hole "s_code" -Eigenschaftswert aus der ersten JSON-Datei und muss mit dem zweiten JSON- "handleScode" -Eigenschaftswert verglichen werden . Wenn ein passender Fall vorliegt, müssen Sie den Wert der stockState-Eigenschaft vom zweiten JSON anzeigen.

Jede Hilfe wäre willkommen.

Code:

$scope.showDataMemory = function(item){ 
     $('.'+item).addClass('active').removeClass('hide'); 
     $('.'+item).siblings('div.device_overview').removeClass('active').addClass('hide');   

**var result111 = $scope.deviceList.devices.map(function(device) { 
    return device.variants.map(function(variant) { 
    return stocks.filter(function(stock){ 
     return stock.handsetScode === variant.s_code; 
    })[0].stockState;  
    }); 
}); 
alert(result111);** 


    } 

Antwort

0

dieses Arbeitsbeispiel Versuchen. Es wird nach Ihrer Anforderung arbeiten.

var firstJSON = 
 
      { 
 
     "devices": [ 
 
     { 
 
      "sub_family": "6s Plus", 
 
      "presales_description": "iPhone 6 Plus features a stunning 5.5-inch Retina HD display, amazing cameras and many advanced features, all in a breakthrough design. A 5.5-inch Retina HD display. An A8 chip with 64-bit desktop-class architecture. 8MP iSight camera with Focus Pixels and optical image stabilisation. Touch ID. Fast 4G LTE and Wi-Fi.1 Long battery life.2 And iOS 9 and iCloud.", 
 
      "Camera": true, 
 
      "device_type": "smartphone", 
 
      "presales_vendor": "Apple", 
 
      "sim_form-factor": "Nano", 
 
      "memory": 64, 
 
      "device_class": "HC14", 
 
      "TV Capable": true, 
 
      "family": "iPhone", 
 
      "specification_reference": "D20", 
 
      "variants": [ 
 
      { 
 
       "colour": "Space Grey", 
 
       "default": true, 
 
       "presales_sequence": 961, 
 
       "s_code": "S0346715", 
 
       "credit_risk": 750, 
 
       "colour_code": "#898989", 
 
       "max_credit_term": 24, 
 
       "list_price": 759, 
 
       "presales_start_date": "02/04/2016" 
 
      }, 
 
      { 
 
       "colour": "Silver", 
 
       "default": true, 
 
       "presales_sequence": 961, 
 
       "s_code": "S0346717", 
 
       "credit_risk": 750, 
 
       "colour_code": "#e5e5e5", 
 
       "max_credit_term": 24, 
 
       "list_price": 759, 
 
       "presales_start_date": "02/04/2016" 
 
      } 
 
      ] 
 
     } 
 
     ] 
 
    }; 
 
      
 
    var secondJSON = { 
 
     "stocks":[ 
 
      { 
 
      "estAvailableDate":"", 
 
      "quantityAvailable":"0", 
 
      "obsolete":"false", 
 
      "handsetScode":"S0346717", 
 
      "backOrderable":"Y", 
 
      "stockState":"OutOfStock", 
 
      "stock_update":"13/10/2016 10:57", 
 
      "preorder":true, 
 
      "stock_next_amount":"0" 
 
      }, 
 
      { 
 
      "estAvailableDate":"", 
 
      "quantityAvailable":"0", 
 
      "obsolete":"false", 
 
      "handsetScode":"S0346715", 
 
      "backOrderable":"Y", 
 
      "stockState":"OutOfStock", 
 
      "stock_update":"13/10/2016 10:57", 
 
      "preorder":true, 
 
      "stock_next_amount":"0" 
 
      } 
 
     ] 
 
     };   
 

 
var newArr = [] 
 
    for (var i in firstJSON.devices[0].variants) { 
 
     for (var j in secondJSON.stocks) { 
 
     if(firstJSON.devices[0].variants[i].s_code == secondJSON.stocks[j].handsetScode) { 
 
      newArr.push(secondJSON.stocks[j].stockState); 
 
     } 
 
     } 
 
    } 
 
    
 
console.log(newArr);

+0

Danke für die Antwort .. Ich werde es testen und zurück zu dir – user3774781

+0

Hey Rohit..Ich habe versucht, ur-code, ich denke, es ist nur die erste stockState-Eigenschaft zu vergleichen Wert mit der zweiten JSON-Datei. Tatsächlich habe ich mehr als 20 Array-Werte in meinen beiden JSON-Dateien, basierend auf Ihrem Code wird nur der erste Wert durchlaufen. – user3774781

+0

@ user3774781 Ich änderte den Code jetzt gemäß Ihrer Anforderung. Bitte markieren Sie es richtig und upvote es, wenn es Ihre Erwartungen erfüllt. –

0

Sie können etwas tun.
Auch Ihr JSON scheint unvollständig zu sein, also nahm ich ihre Strukturen etwa so an.

var devices = [ 
 
    { 
 
     "sub_family": "6s Plus", 
 
     "presales_description": "iPhone 6 Plus features a stunning 5.5-inch Retina HD display, amazing cameras and many advanced features, all in a breakthrough design. A 5.5-inch Retina HD display. An A8 chip with 64-bit desktop-class architecture. 8MP iSight camera with Focus Pixels and optical image stabilisation. Touch ID. Fast 4G LTE and Wi-Fi.1 Long battery life.2 And iOS 9 and iCloud.", 
 
     "Camera": true, 
 
     "device_type": "smartphone", 
 
     "presales_vendor": "Apple", 
 
     "sim_form-factor": "Nano", 
 
     "memory": 64, 
 
     "device_class": "HC14", 
 
     "TV Capable": true, 
 
     "family": "iPhone", 
 
     "specification_reference": "D20", 
 
     "variants": [ 
 
     { 
 
      "colour": "Space Grey", 
 
      "default": true, 
 
      "presales_sequence": 961, 
 
      "s_code": "S0346715", 
 
      "credit_risk": 750, 
 
      "colour_code": "#898989", 
 
      "max_credit_term": 24, 
 
      "list_price": 759, 
 
      "presales_start_date": "02/04/2016" 
 
     }, 
 
     { 
 
      "colour": "Silver", 
 
      "default": true, 
 
      "presales_sequence": 961, 
 
      "s_code": "S0346717", 
 
      "credit_risk": 750, 
 
      "colour_code": "#e5e5e5", 
 
      "max_credit_term": 24, 
 
      "list_price": 759, 
 
      "presales_start_date": "02/04/2016" 
 
     }, 
 
     ] 
 
    }, 
 
]; 
 

 
var stocks = [ 
 
     { 
 
     "estAvailableDate":"", 
 
     "quantityAvailable":"0", 
 
     "obsolete":"false", 
 
     "handsetScode":"S0346717", 
 
     "backOrderable":"Y", 
 
     "stockState":"OutOfStock", 
 
     "stock_update":"13/10/2016 10:57", 
 
     "preorder":true, 
 
     "stock_next_amount":"0" 
 
     }, 
 
     { 
 
     "estAvailableDate":"", 
 
     "quantityAvailable":"0", 
 
     "obsolete":"false", 
 
     "handsetScode":"S0346715", 
 
     "backOrderable":"Y", 
 
     "stockState":"OutOfStock", 
 
     "stock_update":"13/10/2016 10:57", 
 
     "preorder":true, 
 
     "stock_next_amount":"0" 
 
     }, 
 
]; 
 

 

 
    
 
    // Main function 
 
var result = devices.map(function(device) { 
 
    return device.variants.map(function(variant) { 
 
    return stocks.filter(function(stock){ 
 
     return stock.handsetScode === variant.s_code; 
 
    }).map(function(stock) { 
 
     return stock.stockState; 
 
    });  
 
    }); 
 
}); 
 
console.log(result);

+0

Hallo Abhishek, nachdem Sie den Code im bekommen die folgenden Fehler Umsetzung: Typeerror: kann Eigenschaft 'stockState' undefinierter unter http: // localhost/Angular_POC (2016.02.11) /js/app%20-%20Copy.js:2904:10 bei Array.map (nativ) bei http: // localhost/Angular_POC (02-11-2016)/js/app% 20-% 20Copy. js: 2901: 26 bei Array.map (native) ... – user3774781

+0

@ user3774781 Können Sie den Code, wo Sie diesen Fehler erhalten? – abhishekkannojia

+0

Ich bekomme diesen Fehler in der Konsole – user3774781

Verwandte Themen