JSON值比较并显示兄弟属性值

时间:2016-11-10 06:23:19

标签: javascript json

我有两个JSON文件:

第一个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"
        }
     ]
   } 
]

}

第二个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"
      }
    ]
  }

我需要获取" s_code"来自第一个JSON文件的属性值,需要与第二个JSON" phonesScode"进行比较。适当的价值。如果存在匹配的大小写,则需要显示第二个JSON中的stockState属性值。

任何帮助将不胜感激。

代码:

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


    }

2 个答案:

答案 0 :(得分:0)

你可以这样做。
你的JSON似乎也不完整,所以我假设他们的结构是这样的。

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

答案 1 :(得分:0)

试试这个工作示例。它将按照您的要求工作。



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