两种常用遍历方式

一,jQuery   each() 方法

示例:

$("button").click(function(){
  $("li").each(function(){
    alert($(this).text())
  });
});

each() 方法规定为每个匹配元素规定运行的函数。

提示:返回 false 可用于及早停止循环。

开发环境:

遍历多选框,获取选中的所有框的值:

 $("input:checkbox[name='stage']:checked").each(function() {
            $scope.areaAIdList.push(Number($(this).val()));
        });

二,js  .forEach

var arr=[1,2,3,4];
arr.forEach(function(val,index,arr){//val为数组中当前的值,index为当前值的下标,arr为原数组
    arr[index]=2*val;
});

在angular开发中:

angular.forEach($scope.details.areaAId,function (data) {
                $http({
                    method:"GET",
                    url:basePath+"/area/getAreaById?areaId="+data,
                }).then(function succese(response) {
                    $scope.countrysList.push(response.data.area)
                    console.log($scope.countrysList)
                });
            })

可以参考其他文章:

https://www.cnblogs.com/longailong/p/6409172.html

猜你喜欢

转载自blog.csdn.net/qq_23521659/article/details/80965313