user3774781 user3774781 - 27 days ago 8
JSON Question

JSON value comparing and display the sibling property value

I have two JSON files:

First 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"
}
]
}
]


}

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 to fetch "s_code" property value from first JSON file and need to compare with the second JSON "handsetScode" property value. If there is a matching case, then need to display the stockState property value from the second JSON.

Any help would be 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);**


}

Answer

Try this working example. It will work as per your requirement.

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);