简体   繁体   English

为什么嵌套for循环中的代码无法执行?

[英]Why won't my code within my nested for loop get executed?

I have two objects. 我有两个对象。 One with lots of data about persons (like born, name, birthplace, etc.) and one with an array for each character in the alphabet. 一个带有大量有关人的数据(例如出生,姓名,出生地等),另一个带有数组中每个字母的字符。 My goal is to get through every person in my persons object and compare the first letter of the persons name with the properties of the alphabet object. 我的目标是了解我的人员对象中的每个人,并将人员名称的第一个字母与字母对象的属性进行比较。 So that I can sort the persons names alphabetical in my alphabet object through a push operation. 这样我就可以通过推送操作对字母对象中按字母顺序排列的人员姓名进行排序。 This is what I came up with. 这就是我想出的。 The problem is, that the code within my 2nd loop just won't get executed and I can't find the problem. 问题是,我的第二个循环中的代码将无法执行,并且我找不到问题。

angular.module('ArtistsCtrl', []).controller('ArtistsController', ['$scope', 'Artists', function ($scope, Artists) {

$scope.data = {};

$scope.tagline = 'A';

Artists.query(function (res) {
    $scope.data.artists = res;
    $scope.sortedNames = {A: [], B: [], C: [], D: [], E: [], F: [], G: [], H: [], I: [], J: [] , K: [], L: [], M: [], N: [], O: [], P: [], Q: [], R: [], S: [], T: [], U: [], V: [], W: [], X: [], Y: [], Z: []};
    var sortedNamesProperties = Object.getOwnPropertyNames($scope.sortedNames);

    (function () {
        //loop to go through my artists array
        for(var i = 0; i <= $scope.data.artists.length; i++){
            //loop to go through my sortedNames array
            for(var z = 0; z <= $scope.sortedNames.length; z++){
                if($scope.data.artists[i].displayname.charAt(0) == sortedNamesProperties[z]){
                    $scope.sortedNames[z].push($scope.data.artists[i]);
                    break;
                }
            }
        }
    })()
});

}]);

EDIT: 编辑:

Thank you for all the possibilities you posted. 感谢您发布的所有可能性。 The hint that I deal with an obj and not an array solved it :) I rewrote the code in the following way, although they are may be some better and more efficient ways to deal with it. 我处理的是obj而不是数组的提示解决了它:)我以以下方式重写了代码,尽管它们可能是更好和更有效的处理方式。 But thanks a lot for your help. 但是非常感谢您的帮助。 Especially @ Andy 尤其是@安迪

angular.module('ArtistsCtrl', []).controller('ArtistsController', ['$scope', 'Artists', function ($scope, Artists) {

$scope.data = {};

$scope.tagline = 'A';

Artists.query(function (res) {
    $scope.data.artists = res;
    $scope.sortedNames = {A: [], B: [], C: [], D: [], E: [], F: [], G: [], H: [], I: [], J: [] , K: [], L: [], M: [], N: [], O: [], P: [], Q: [], R: [], S: [], T: [], U: [], V: [], W: [], X: [], Y: [], Z: []};
    var sortedNamesProperties = Object.getOwnPropertyNames($scope.sortedNames);

    (function () {
        //loop to go through my artists array
        for(var i = 0; i <= $scope.data.artists.length; i++){
            //loop to go through my sortedNames array
            for(var z = 0; z <= Object.keys($scope.sortedNames).length; z++){
                if($scope.data.artists[i].displayname.charAt(0) == sortedNamesProperties[z]){
                    $scope.sortedNames[sortedNamesProperties[z]].push($scope.data.artists[i]);
                    break;
                }
            }
        }
    })()
});

}]);

Instead of the nested loop, you can address the fields of $scope.sortedNames object as keys in a map: 除了嵌套循环,您还可以将$scope.sortedNames对象的字段作为映射中的键进行$scope.sortedNames

function () {
    //loop to go through my artists array
    for (var i = 0; i < $scope.data.artists.length; i++) {        
        $scope.sortedNames[$scope.data.artists[i].displayname.charAt(0)].push($scope.data.artists[i]);
    }
}

Somewhat simplified fiddle: http://jsfiddle.net/9xeS8/1/ 简化的小提琴: http : //jsfiddle.net/9xeS8/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM