Javascript 如何获取for循环中的所有值

Javascript 如何获取for循环中的所有值,javascript,angularjs,Javascript,Angularjs,当我发出警报时,我只得到id的最后一个值。我找不到哪里出错了 $scope.addToList = function (products,qty) { if ($scope.order.length > 0) { for (var i = 0; i < $scope.order.length; i++) { $scope.Thx=$scope.order[i].id; }}}; alert($scop

当我发出警报时,我只得到id的最后一个值。我找不到哪里出错了

  $scope.addToList = function (products,qty) {
    if ($scope.order.length > 0) {
        for (var i = 0; i < $scope.order.length; i++) {
            $scope.Thx=$scope.order[i].id;
            }}};

   alert($scope.Thx);
$scope.addToList=功能(产品、数量){
如果($scope.order.length>0){
对于(变量i=0;i<$scope.order.length;i++){
$scope.Thx=$scope.order[i].id;
}}};
警报($scope.Thx);

首先将
$scope.Thx
声明为数组

$scope.Thx = [];

$scope.addToList = function (products, qty) {
    if ($scope.order.length > 0) {
        for (var i = 0; i < $scope.order.length; i++) {
            $scope.Thx.push($scope.order[i].id);
        }
    }
};
$scope.Thx = [];
$scope.addToList = function (products, qty) {
if ($scope.order.length > 0) {
    for (var i = 0; i < $scope.order.length; i++) {
        $scope.Thx.push($scope.order[i].id);
    }
}
};
var tempVar="";
for(var i=0; i<$scope.Thx.length;i++)
{
    tempVar += $scope.Thx[0]+"\n";
}
alert(tempVar);
$scope.Thx=[];
$scope.addToList=功能(产品、数量){
如果($scope.order.length>0){
对于(变量i=0;i<$scope.order.length;i++){
$scope.Thx.push($scope.order[i].id);
}
}
};

首先将
$scope.Thx
声明为数组

$scope.Thx = [];

$scope.addToList = function (products, qty) {
    if ($scope.order.length > 0) {
        for (var i = 0; i < $scope.order.length; i++) {
            $scope.Thx.push($scope.order[i].id);
        }
    }
};
$scope.Thx = [];
$scope.addToList = function (products, qty) {
if ($scope.order.length > 0) {
    for (var i = 0; i < $scope.order.length; i++) {
        $scope.Thx.push($scope.order[i].id);
    }
}
};
var tempVar="";
for(var i=0; i<$scope.Thx.length;i++)
{
    tempVar += $scope.Thx[0]+"\n";
}
alert(tempVar);
$scope.Thx=[];
$scope.addToList=功能(产品、数量){
如果($scope.order.length>0){
对于(变量i=0;i<$scope.order.length;i++){
$scope.Thx.push($scope.order[i].id);
}
}
};

对于循环中的每个迭代,此行将覆盖
$scope.Thx

$scope.Thx=$scope.order[i].id;
现在有两种方法可以得到想要的结果。 1.如果要为阵列中的每个项目分别发送警报,请执行以下操作:

$scope.addToList = function (products,qty) {
if ($scope.order.length > 0) {
    for (var i = 0; i < $scope.order.length; i++) {
        $scope.Thx=$scope.order[i].id;
        alert($scope.Thx);
        }}};

此行将覆盖循环中每个迭代的
$scope.Thx

$scope.Thx=$scope.order[i].id;
现在有两种方法可以得到想要的结果。 1.如果要为阵列中的每个项目分别发送警报,请执行以下操作:

$scope.addToList = function (products,qty) {
if ($scope.order.length > 0) {
    for (var i = 0; i < $scope.order.length; i++) {
        $scope.Thx=$scope.order[i].id;
        alert($scope.Thx);
        }}};

你认为,
$scope.Thx=$scope.order[i].id正在做什么?在循环内部,它在每个循环上重置$scope.Thx的值。因此,当您在循环后发出警报时,它将是最后一个值。
$scope.Thx
应该是一个数组。然后用
$scope.Thx.push(…)
将值附加到它上面
$scope.Thx=$scope.order[i].id正在做什么?在循环内部,它在每个循环上重置$scope.Thx的值。因此,当您在循环后发出警报时,它将是最后一个值。
$scope.Thx
应该是一个数组。然后使用
$scope.Thx.push(…)将值附加到它上。