Javascript 如何获得ng repeat内数组所有索引的第一个元素的和?
我想得到ng repeat中数组所有索引的第一个元素的和。我想声明一个变量,并想在每次迭代ng repeat时添加值。并希望在末尾显示该变量。下面是我的代码Javascript 如何获得ng repeat内数组所有索引的第一个元素的和?,javascript,html,angularjs,angularjs-ng-repeat,Javascript,Html,Angularjs,Angularjs Ng Repeat,我想得到ng repeat中数组所有索引的第一个元素的和。我想声明一个变量,并想在每次迭代ng repeat时添加值。并希望在末尾显示该变量。下面是我的代码 $scope.test_array = [{qty:5, unit:'each'}, {qty:5, unit:'each'}, {qty:5, unit:'each'}, {qty:5, unit:'each'},
$scope.test_array = [{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'}];
<tr ng-repeat="x in test_array ">
<td> {{x.qty}} </td>
</tr>
<!-- I want sum of all qty of each index -->
<tr ng-repeat="x in test_array ">
<td>
{{test_array[0].qty + test_array[1].qty + test_array[2].qty + test_array[3].qty and so on... }} </td>
</tr>
Result should be like below.
35
35
35
35
35
35
35
I also want to get that value in javascript.
console.log("first Total" + $scope.first_total); //35
console.log("second Total" + $scope.second_total); //35
And so on...
I read about *ngFor but I am not sure it can be used here or not
$scope.test_数组=[{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'}];
{{x.qty}
{{测试数组[0]。数量+测试数组[1]。数量+测试数组[2]。数量+测试数组[3]。数量等…}
结果应该如下所示。
35
35
35
35
35
35
35
我还希望在javascript中获得该值。
console.log(“首次总计”+$scope.first_Total)//35
log(“第二总计”+$scope.second_总计)//35
等等
我读过关于*ngFor的文章,但我不确定它是否可以在这里使用
js
$scope.test_数组=[{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'},
{数量:5,单位:'each'}];
让newArr=[]
对于(设i=0;i<$scope.test_array.length;i++){
for(输入$scope.test_数组[i]){
如果(键==‘数量’){
newArr.push($scope.test\u数组[i][key])
}
}
}
$scope.sum=newArr.reduce(函数(a,b){
返回a+b;
}, 0);
html
{{sum}}
$scope.test_array = [{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'},
{qty:5, unit:'each'}];
let newArr = []
for(let i = 0; i < $scope.test_array.length; i++){
for(let key in $scope.test_array[i]){
if(key === 'qty'){
newArr.push($scope.test_array[i][key])
}
}
}
$scope.sum = newArr.reduce(function(a, b){
return a + b;
}, 0);
<tr ng-repeat="x in test_array ">
<td>
{{sum}}
</td>
</tr>