如何检查json数组中的字符串

时间:2017-02-03 12:29:55

标签: angularjs json ionic-framework

console.log($scope.filteredOffers);嗨,所有人都希望检查json中的字符串,就像来自params的字符串现在想要检查整个json并只显示与之相关的数据。请检查我的代码..换句话说,想要根据来自url(即将到来)的参数来显示数据。

.controller('mixCtrl', function($scope,$http,$stateParams) {


     $http.get("http://tools.vcommission.com/api/coupons.php?apikey=e159f64e3dd49fddc3bb21dcda70f10c6670ea91aac30c7cb1d4ed37b20c45b8").then(function (response) {
      $scope.myData = response.data;
      $scope.offerName = ''; //set initially
      $scope.selectedIndex = -1;
      $scope.filteredOffers = [];
     // $scope.link1 = [];
     $scope.da = $stateParams.offer_name;
     var a =  $scope.da;
 console.log(a);

      $scope.filteredOffers = $scope.myData.filter(function(a) {
        for (var i=0;i<$scope.myData.length;i++)
        {
                $link =$scope.myData[i].offer_name;
            if (a==$link)
            {
                return a ;
                console.log(a );

            }

            //console.log(a );
            }
       // return offer.offer_name == $scope.da;
        console.log($scope.da);

      });
  });


  /*
 $scope.showData = function(offer_name, index) {
        $scope.offerName = offer_name;
      $scope.filteredOffers = $scope.myData.filter(function(offer) {
        return offer.offer_name == $scope.offerName;
      });
        $scope.selectedIndex = index;

      }*/
      $scope.dealopen = function(a){
            for (var i=0;i<$scope.myData.length;i++)
        {
            //console.log($scope.data[i].name);
            $link=$scope.data[i].name;
            console.log($link);
            if ($link==$a)
            {

            $window.open($link,"_self","location=yes"); 
            //console.log($a);
            }




        }



        }


})

Html

<div ng-repeat="offer in filteredOffers">
      <div class="couponCode">{{offer.coupon_code}}</div>
      <div class="couponTitle">{{offer.coupon_title}}</div>
      <div class="couponDescription">{{offer.coupon_Description}}</div>
    </div>

2 个答案:

答案 0 :(得分:0)

你错误地使用了Array.filter()函数!

之前:

$scope.filteredOffers = $scope.myData.filter(function(a) {
        for (var i=0;i<$scope.myData.length;i++)
        {
                $link =$scope.myData[i].offer_name;
            if (a==$link)
            {
                return a ;
                console.log(a );

            }

            //console.log(a );
            }
       // return offer.offer_name == $scope.da;
        console.log($scope.da);

      });

之后(根据正确的语法):

$scope.filteredOffers = $scope.myData.filter(function(a) {                    
            if (check condition)
            {
                return true ;// when true is returned the json held in a gets pushed into the filteredOffers array                
            }
            else{
                return false;//when false is returned, the json held in 'a' is ignored and not pushed into the filteredOffers array
            }      
      });

Array.filter's Doc Link 但是你使用的逻辑有问题。如果你能准确地告诉我你想要做什么,我或许可以帮助你。

答案 1 :(得分:0)

如果您经常在应用程序中处理json和数组,那么在您的应用程序中集成loadash或underscore会更好。

在加载控制器文件之前,将以下代码添加到HTML中。

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

查看http://tools.vcommission.com/api/coupons.php?apikey=e159f64e3dd49fddc3bb21dcda70f10c6670ea91aac30c7cb1d4ed37b20c45b8的响应后,下面的代码将为您提供与offer_name匹配的数组。

     $http.get("http://tools.vcommission.com/api/coupons.php?apikey=e159f64e3dd49fddc3bb21dcda70f10c6670ea91aac30c7cb1d4ed37b20c45b8").then(function (response) {

      $scope.filteredOffers = [];
      var offerName = $stateParams.offer_name;
      $scope.filteredOffers = _.filter(response.data, ["offer_name",offerName]);

      })