gpt4 book ai didi

javascript - 在angularjs中计算json数据

转载 作者:行者123 更新时间:2023-12-02 14:06:35 25 4
gpt4 key购买 nike

我正在通过 angularjs(不是 2)学习,并试图找出如何计算客户订单总额。

我的数据如下所示:

var customers = [
{
id: 1,
joined: '2000-12-02',
name:'John',
city:'Chandler',
orders: [
{
id: 1,
product: 'Shoes',
total: 9.9956
}
]
},
{
id: 2,
joined: '1965-01-25',
name:'Zed',
city:'Las Vegas',
orders: [
{
id: 2,
product: 'Baseball',
total: 9.995
},
{
id: 3,
product: 'Bat',
total: 9.995
}
]
}
]

我有一个工厂和一个接收该数据的 Controller ......

我的 Controller 看起来像:

(function() {

var CustomersController = function ($scope, $log, customerFactory) {
$scope.sortBy = 'name';
$scope.reverse = false;
$scope.customers = [];

function init () {

customerFactory.getCustomers()
.success(function(customers) {
$scope.customers = customers;

})
.error(function(data, status, headers, config){
$log.log(data.error + ' ' + status );

});

}

init();

$scope.doSort = function(propName) {
$scope.sortBy = propName;
$scope.reverse = !$scope.reverse;
};


};

CustomersController.$inject = ['$scope','$log' ,'customerFactory'];

angular.module('customersApp')
.controller('CustomersController', CustomersController);

}());

我的观点如下:

<h2>Customers</h2>
Filter: <input type="text" ng-model="customerFilter.name" />
<br /><br />
<table>
<tr>
<th ng-click="doSort(name)">Name</th>
<th ng-click="doSort(city)">City</th>
<th ng-click="doSort(joined)">Joined</th>
<th>&nbsp;</th>
</tr>
<tr ng-repeat="cust in customers | filter:customerFilter | orderBy:sortBy:reverse">
<td>{{ cust.name }}</td>
<td>{{ cust.city }}</td>
<td><a href="#/orders/{{ cust.employeeid }}">View Orders</a></td>
</tr>
</table>
<br />
<span>Total customers: {{ customers.length }} </span>

我知道如何在订单总计列中添加...但是给定返回的 json 如何计算它?

---------------编辑--------------

我认为这正朝着正确的方向发展?

    customerFactory.getCustomers()
.success(function(customers) {
for(var i = 0; i < customers.length; i++) {
var currCustomer = customers[i];
var aCustomerTotal = 0;

if (currCustomer.hrs) {

for (var j = 0; j < currCustomer.hrs.length; j++) {
aCustomerTotal += parseInt(currCustomer.hrs[j].bllhrs);
}
} else {
aCustomerTotal=0
}
customers[i].orderTotal = aCustomerTotal;
console.log(currCustomer.lastname + " total: " + aCustomerTotal);
}
// after the exeuction of this logic set your $scope's customer to the modified object.
$scope.customers = customers;
})

最佳答案

如果您所坚持的只是获取每个客户的总数 -> 您可以使用简单的双 for 循环:

.success(function(customers) {
for(var i = 0; i < customers.length; i++) {
var currCustomer = customers[i];
var aCustomerTotal = 0;
for (var j = 0; j < currCustomer.orders.length; j++) {
aCustomerTotal += currCustomer.orders[j].total;
}
customers[i].orderTotal = aCustomerTotal;
console.log(currCustomer.name + " total: " + aCustomerTotal);
}
// after the exeuction of this logic set your $scope's customer to the modified object.
$scope.customers = customers;
}

关于javascript - 在angularjs中计算json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40006348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com