如果JSON文件中存在常见值,则显示文本

时间:2018-01-09 11:15:03

标签: javascript html angularjs json

test.json:

    {
  "test": [
    {
      "test1": "NOF",
      "scode": "S0354604"
    },
    {
      "test2": "NOF",
      "scode": "S03546041"
    },
    {
      "test3": "NOF",
      "scode": "S0348339"
    }   
    ]
    }

devices.json:

{
  "devices": [
    {
      "sub_family": "Phone by Google",
      "presales_description": "Meet Pixel, Phone by Google. It has the highest rated smartphone camera. Ever. A battery that lasts all day. Unlimited storage for all your photos and videos. And it's the first phone with the Google Assistant built in.",
      "Camera": true,
      "device_type": "smartphone",
      "presales_vendor": "Google",
      "sim_form-factor": "Nano",
      "memory": 32,
      "device_class": "HC10",
      "TV Capable": true,
      "family": "Pixel XL",
      "specification_reference": "D47",
      "variants": [
        {
          "colour": "Quite Black",
          "min_upfront_amount": 0,
          "default": true,
          "presales_sequence": 956,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0354604",
          "credit_risk": 750,
          "colour_code": "#000000",
          "max_credit_term": 24,
          "list_price": 789,
          "presales_start_date": "22/11/2016"
        }
      ],
      "Music Player": true,
      "NFC": true,
      "presales_name": "Pixel XL",
      "device_group": "A"
    },
    {
      "sub_family": "6s",
      "presales_description": "Smooth, continuous unibody design. 3D Touch, Live Photos, an A9 chip, advanced cameras, and a 4.7-inch Retina HD display. And so much more.",
      "Camera": true,
      "device_type": "smartphone",
      "presales_vendor": "Apple",
      "sim_form-factor": "Nano",
      "memory": 32,
      "device_class": "HC37",
      "TV Capable": true,
      "family": "iPhone",
      "specification_reference": "D44",
      "variants": [
        {
          "colour": "Gold",
          "default": false,
          "presales_sequence": 462,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350618",
          "credit_risk": 750,
          "colour_code": "#d9cdbd",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        },
        {
          "colour": "Rose Gold",
          "default": false,
          "presales_sequence": 462,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350667",
          "credit_risk": 750,
          "colour_code": "#d0a8a0",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        },
        {
          "colour": "Silver",
          "default": false,
          "presales_sequence": 464,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350671",
          "credit_risk": 750,
          "colour_code": "#d1d3d2",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        },
        {
          "colour": "Space Grey",
          "default": true,
          "presales_sequence": 463,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350669",
          "credit_risk": 750,
          "colour_code": "#848889",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        }
      ],
      "Music Player": true,
      "NFC": true,
      "presales_name": "iPhone 6s",
      "device_group": "A"
    }
  ]
}

app.js:

    for(var k = 0; k < $scope.devicesList.length; k++){
        if($scope.callplanList[j].upfront_cost[a].handsetClass == $scope.devicesList[k].device_class.toLowerCase()){                                    
            //console.log($scope.devicesList[k].device_class);
            $scope.arr.push($scope.devicesList[k]);
            $scope.arr = $scope.arr.filter(function(elem, index, self){ return self.indexOf(elem) == index });

        }
        for(var b = 0; b < $scope.testing.length; b++){                                 
            if($scope.devicesList[k].variants.s_code == $scope.testing[b].scode){
                $scope.arr.push($scope.testing[b].scode);
                //console.log(scope.devicesList[k].variants.s_code);
            }

        }
    }   

HTML:

<div class="imgDiv">
<img ng-repeat="image in x.variants" ng-show="$first" class="{{image.s_code}}-{{x.presales_name.split(' ').join('-').replace('(','').replace(')','')}}" src="https://img01.bt.co.uk/mobile/images/phones/scode/sm/{{image.s_code.toLowerCase()}}.png"/>         
<!-- <span>Welcome</span> -->
</div>      

我需要将 devices.json test.json 进行比较,如果 test.json 中有任何常见的scode,则以上span标签应显示在相应的div中。

我是棱角分明的新人,任何帮助都会受到赞赏。

  

注意:我没有附加其他JSON文件和代码,因为这样   功能不是必需的。

1 个答案:

答案 0 :(得分:0)

其中一个解决方案就是过滤掉您想要在视图中显示的数据。您可以更好地优化代码。

&#13;
&#13;
var testjson =  {
  "test": [
    {
      "test1": "NOF",
      "scode": "S0354604"
    },
    {
      "test2": "NOF",
      "scode": "S03546041"
    },
    {
      "test3": "NOF",
      "scode": "S0348339"
    }   
    ]
    }




var deviceJson = {
  "devices": [
    {
      "sub_family": "Phone by Google",
      "presales_description": "Meet Pixel, Phone by Google. It has the highest rated smartphone camera. Ever. A battery that lasts all day. Unlimited storage for all your photos and videos. And it's the first phone with the Google Assistant built in.",
      "Camera": true,
      "device_type": "smartphone",
      "presales_vendor": "Google",
      "sim_form-factor": "Nano",
      "memory": 32,
      "device_class": "HC10",
      "TV Capable": true,
      "family": "Pixel XL",
      "specification_reference": "D47",
      "variants": [
        {
          "colour": "Quite Black",
          "min_upfront_amount": 0,
          "default": true,
          "presales_sequence": 956,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0354604",
          "credit_risk": 750,
          "colour_code": "#000000",
          "max_credit_term": 24,
          "list_price": 789,
          "presales_start_date": "22/11/2016"
        }
      ],
      "Music Player": true,
      "NFC": true,
      "presales_name": "Pixel XL",
      "device_group": "A"
    },
    {
      "sub_family": "6s",
      "presales_description": "Smooth, continuous unibody design. 3D Touch, Live Photos, an A9 chip, advanced cameras, and a 4.7-inch Retina HD display. And so much more.",
      "Camera": true,
      "device_type": "smartphone",
      "presales_vendor": "Apple",
      "sim_form-factor": "Nano",
      "memory": 32,
      "device_class": "HC37",
      "TV Capable": true,
      "family": "iPhone",
      "specification_reference": "D44",
      "variants": [
        {
          "colour": "Gold",
          "default": false,
          "presales_sequence": 462,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350618",
          "credit_risk": 750,
          "colour_code": "#d9cdbd",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        },
        {
          "colour": "Rose Gold",
          "default": false,
          "presales_sequence": 462,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350667",
          "credit_risk": 750,
          "colour_code": "#d0a8a0",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        },
        {
          "colour": "Silver",
          "default": false,
          "presales_sequence": 464,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350671",
          "credit_risk": 750,
          "colour_code": "#d1d3d2",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        },
        {
          "colour": "Space Grey",
          "default": true,
          "presales_sequence": 463,
          "euEligibleEndDate": "",
          "euEligible": "",
          "s_code": "S0350669",
          "credit_risk": 750,
          "colour_code": "#848889",
          "max_credit_term": 0,
          "list_price": 549,
          "presales_start_date": "01/04/2016"
        }
      ],
      "Music Player": true,
      "NFC": true,
      "presales_name": "iPhone 6s",
      "device_group": "A"
    }
  ]
}

var filtertedJson = [];
    testjson.test.forEach(function(test){
         deviceJson.devices.forEach(function(device){
              device.variants.forEach(function(vr){
               if(test.scode ==vr.s_code ){
               console.log(vr.s_code,"matching s_code")
               filtertedJson.push(device);
           }
             })
   
        })
       
   })
   
   
   console.log(filtertedJson,"filtertedJson")
&#13;
&#13;
&#13;