Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查对象数组是否为空?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何检查对象数组是否为空?

Javascript 如何检查对象数组是否为空?,javascript,angularjs,Javascript,Angularjs,我尝试过以下几种方法: var app = angular.module('app',['ui.bootstrap']); app.controller("ListCtrl", function ($scope, $http) { $scope.submit = function () { $scope.loading = true; $scope.error = false; $http.get('http://www.omdbapi

我尝试过以下几种方法:

var app = angular.module('app',['ui.bootstrap']);

app.controller("ListCtrl", function ($scope, $http) {

    $scope.submit = function () {
        $scope.loading = true;
        $scope.error = false;
        $http.get('http://www.omdbapi.com/?s=' + $scope.search + '&r=json')
               .then(function (res) {
                    var titles = [];
                    angular.forEach(res.data.Search, function(item){
                       $http.get('http://www.omdbapi.com/?t=' + item.Title + '&y=&plot=full&r=json').then(function(res){
                       if (res.data.Poster === "N/A") {
                         res.data.Poster = "http://placehold.it/350x450/FF6F59/FFFFFF&text=Image+not+Available!!";
                       }
                        titles.push(res.data); 
                      });  
                    });

                   $scope.movie = titles;
                   $scope.results = true;
                   $scope.error = false;
                   $scope.loading = false;


                   if (titles.length==0) {           // not working
                       $scope.results = false;
                       $scope.error = true;
                   }


               })
它们似乎都不起作用……

$http.get()
是异步的,因此如果(titles.length==0){立即执行语句

使用计数器来确定何时解析所有承诺,然后执行检查。将if语句移到回调中

Object.getOwnPropertyNames(titles).length === 0) 
obj == null 

发生这种情况的原因是范围不正确

var titles=[];
中定义。然后

您正在检查
之外的长度。然后

由于
标题
之外不可用,因此
将不起作用。(
未定义。长度==0

解决方案:

 var count = res.data.Search.length;

 angular.forEach(res.data.Search, function(item){
   $http.get('http://www.o....rest of code').then(function(res) {
      // rest of code

      titles.push(res.data); 
      if (!count-- && !titles.length) {
          $scope.results = false;
          $scope.error = true;
        }
      }
    });
 });

对你来说,支票

.then(function (res) {
                    var titles = [];
                    angular.forEach(res.data.Search, function(item){
                       $http.get('http://www.omdbapi.com/?t=' + item.Title + '&y=&plot=full&r=json').then(function(res){
                       if (res.data.Poster === "N/A") {
                         res.data.Poster = "http://placehold.it/350x450/FF6F59/FFFFFF&text=Image+not+Available!!";
                       }
                        titles.push(res.data); 
                      }); 
               $scope.movie = titles;
               $scope.results = true;
               $scope.error = false;
               $scope.loading = false;


               if (titles.length==0) {           // now this will work
                   $scope.results = false;
                   $scope.error = true;
               }  
    });//titles will not be available after this.
将在

titles.length 
因为您使用的是稍后将返回的异步请求。
您需要将您的陈述打包到请求的回答栏中。

只是作为一个旁白,但对我的练习和您未来的帮助是有益的:

您遇到的部分问题是范围管理(JS scope,而不是Angular$scope),部分问题是并发管理,部分问题似乎是普通的旧范围格式,这使得很难看到所有控制块的开始和结束位置(当不只是if/else,而是回调/承诺时,这会变得很糟糕)

<>这是一个很小的例子,你可以通过对问题的快速重构来考虑解决这些问题:

功能弹拨(键){
返回函数从(obj){return obj[key];};
}
angular.module(“app”、[“ui.bootstrap”]);
angular.moule(“app”).service(“omdbService”),[“$http”,函数($http){
函数getSearch(搜索){
var search=$http.get(“http://www.omdbapi.com/?s=“+search+”&r=json”)
。然后(采集(“数据”);
返回搜索;
}
函数getMovie(标题){
var search=$http.get(“http://www.omdbapi.com/?t=“+title+”&y=&plot=full&r=json”)
。然后(采集(“数据”);
返回搜索;
}
返回{
getSearch:getSearch,
getMovie:getMovie,
GetPlaceholder海报:函数(){return“http://placehold.it/350x450/FF6F59/FFFFFF&text=Image+不可用!!“;}
};
}]);
angular.moule(“app”).controller(“ListCtrl”、[“$scope”、“$q”、“omdbService”、函数($scope、$q、omdb){
函数loadMovie(电影){
返回omdb.getMovie(movie.Title)[“catch”](函数(){return undefined;});
}
函数movieExists(movie){return!!movie;}
函数updatePoster(电影){
movie.Poster=movie.Poster | | omdb.getplaceholder Poster();
回归电影;
}
函数设置结果(电影){
$scope.movie=movies;//$scope.movies?
$scope.results=true;
$scope.error=false;
$scope.loading=false;
}
函数句柄错误(){
$scope.results=false;
$scope.error=true;
}
$scope.submit=函数(){
$scope.loading=true;
$scope.error=false;
omdb.getSearch($scope.search)
。然后(拔出(“搜索”))
.then(函数(movies){return$q.all(movies.map(loadMovie));}
.then(函数(movies){返回movies.filter(movieExists.map(updatePoster);})
。然后(设置结果,handleError);
};

}])
任何工作示例??您能调试并检查长度是否为零吗?我尝试了您的代码。这只是在开始时初始化代码中的所有内容,如错误notif、加载notif等。如果您愿意,我可以发布回购链接供您贡献。我肯定会在接下来的几天内查看,并可能会提交一个P在那之后。
titles.push