javascript - JSON value comparing and display the sibling property value -
i have 2 json files:
first json:
$scope.devicelist = { "devices": [ { "sub_family": "6s plus", "presales_description": "iphone 6 plus features stunning 5.5-inch retina hd display, amazing cameras , many advanced features, in breakthrough design. 5.5-inch retina hd display. a8 chip 64-bit desktop-class architecture. 8mp isight camera focus pixels , optical image stabilisation. touch id. fast 4g lte , wi-fi.1 long battery life.2 , ios 9 , 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" } ] } ]
}
second 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" } ] }
i need fetch "s_code" property value first json file , need compare second json "handsetscode" property value. if there matching case, need display stockstate property value second json.
any appreciated.
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);** }
try working example. work per requirement.
var firstjson = { "devices": [ { "sub_family": "6s plus", "presales_description": "iphone 6 plus features stunning 5.5-inch retina hd display, amazing cameras , many advanced features, in breakthrough design. 5.5-inch retina hd display. a8 chip 64-bit desktop-class architecture. 8mp isight camera focus pixels , optical image stabilisation. touch id. fast 4g lte , wi-fi.1 long battery life.2 , ios 9 , 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 = [] (var in firstjson.devices[0].variants) { (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);
Comments
Post a Comment