【问题标题】:How to check string in json array如何检查json数组中的字符串
【发布时间】:2017-02-03 12:29:55
【问题描述】:

console.log($scope.filteredOffers);hi 所有人都想检查 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>

【问题讨论】:

    标签: angularjs json ionic-framework


    【解决方案1】:

    你错误地使用了 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 但是您使用的逻辑有问题。如果你能确切地告诉我你想做什么,我也许可以帮助你。

    【讨论】:

    • 我想代表 /deal?offer_name 上传递的查询字符串显示数据,应该在 json 文件中搜索并显示他们找到该参数的位置
    【解决方案2】:

    如果您在应用程序中经常处理 json 和数组,那么最好在您的应用程序中集成 loadash 或下划线。

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

    &lt;script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"&gt;&lt;/script&gt;

    查看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]);
    
          })
    

    【讨论】:

    • 他们在参数列表后给出语法错误 SyntaxError: missing )
    • 更新了答案。将此代码复制并粘贴到您的控制器中,这将为您提供所需的内容。
    • 数据仍未显示
    • 确保正确的值传入$stateParams.offer_name。代码运行良好。
    • {{offer.offer_name}}
    猜你喜欢
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    相关资源
    最近更新 更多