通过匹配 - Javascript/AngularJS 中的参数来查找对象

Find Object by matching parameters within - Javascript/AngularJS

本文关键字:参数 对象 查找 AngularJS Javascript      更新时间:2023-09-26

我有一个AngularJS工厂(保存对象的集合),我正在尝试查找用户正在请求的项目。

angular.module('app').factory('APIMethodService', [function() {
  return {
    apis: [
      {
      accounts: [
        {
          parameters : [
            {
              name : "Account",
              version : "1.0"
            }
          ],
          uri: Head + "/v1/accounts/account_number",
          methods : [
            {
              name: "Account Number",
              desc: "Returns the account number."
            }, {
              name: "Account Money",
              desc: "Returns the monetary amount within the account."
            }
          ]
        },
        {
          parameters : [
            {
              name : "Account",
              version : "2.0"
            }
          ],
          uri: Head + "/v2/accounts/account_number",
          methods: [
            {
              name: "Account Number",
              desc: "Returns the account number."
            }, {
              name: "Account Money",
              desc: "Returns the monetary amount within the account."
            }, {
              name: "Account Token",
              desc: "Returns the account's token."
            }
          ]
        }
      ],
      customers:[
        {
          parameters : [
            {
              name : "Customers",
              version : "1.0"
            }
          ],
          uri: Head + "/v1/customers/customer_number",
          methods : [
            {
              name: "Customer Name",
              desc: "Returns the customer's name."
            }, {
              name: "Customer ID",
              desc: "Returns the customer's ID."
            }
          ]
        },
        {
          parameters : [
            {
              name : "Customers",
              version : "2.0"
            }
          ],
          uri : Head + "/v2/customers/customer_number",
          methods: [
            {
              name: "Customer Name",
              desc: "Returns the customer's name."
            }, {
              name: "Customer ID",
              desc: "Returns the customer's ID."
            }, {
              name: "Customer Email",
              desc: "Returns the customer's email."
            }
          ]
        }
      ]
    }
    ]
  };
}]);

控制器

angular.module('app').controller('apiTabController', ['$scope', '$route', '$location', 'APIMethodService', function($scope, $route, $location, APIMethodService) {
  $scope.params = $route.current.params;
  if ($scope.data == null && Object.keys($scope.params).length > 0) {
    console.log("came from URL");
    // Variables I get from URL
    var name = $scope.params.name;
    var version = $scope.params.version;
    // My list of API's
    $scope.currentAPI = APIMethodService.apis;
    for (var i = 0, len = $scope.currentAPI.length; i < len; i++) {
      // FIND THE API OBJECT I NEED
    }    
  }
}]);

所以我从 URL 参数(如 apiTab?name=Customers&version=1.0)中提取名称和版本,然后我试图从我的工厂获取与参数匹配的正确对象。

因此,如果我想要客户,版本 1.0,那将是$scope.currentAPI[0].customers[0],而给我这个的 URL 将是apiTab?name=Customers&version=1.0

但是我不确定如何搜索和匹配正确的工厂对象。

您可以在服务中编写逻辑以获取正确的对象,或者其他可能的解决方案可能是将控制器中的 for 循环替换为以下循环,它仅处理最佳情况:

var matchAPIObject  = {}; // to capture right object
for(var i in currentAPI){
  var targetAPI = currentAPI[i][name.toLowerCase()];
  for(var j in targetAPI){
   //console.log(targetAPI[j].parameters[0].name.toLowerCase()); 
     if(targetAPI[j].parameters[0].name.toLowerCase() === name.toLowerCase()
&& targetAPI[j].parameters[0].version === version ){ 
matchAPIObject = targetAPI[j];  //found it now get me out
        break;
        }
      } 
    }

祝您乐于助人!