AngularJS JavaScript for 循环警告,最多 运行 一次

AngularJS JavaScript for loop warning that will run at most once

为什么 WebStorm 警告我我的 for 循环最多只会 运行 一次?

if (data[i].name) {
    for (var j = 0; j < data[i].name.length; j++) {
        hobby = $scope.users[data[i].name].hobby;
        sport = $scope.users[data[i].name].sport;
        education = $scope.users[data[i].name].education;
        break;
    }
}

问题出在哪里?我在这里错过了什么?

如评论中所述,循环最多只执行一次,因为您 break 在第一次迭代后执行。

但是,从代码来看,您可能根本不需要这里的循环。似乎您想要做的就是检查是否设置了字符串 data[i].name,如果设置了,则根据它检索一些属性,对吗?

如果是这样,请删除循环,因为它对发布的代码段没有任何贡献:

if (data[i].name) {
    hobby = $scope.users[data[i].name].hobby;
    sport = $scope.users[data[i].name].sport;
    education = $scope.users[data[i].name].education;
}

假设data[i].name是一个字符串,data[i].name.length只会给你字符串的长度。