Javascript 双数组和函数

Javascript 双数组和函数,javascript,html,angularjs,Javascript,Html,Angularjs,我必须能够在AngularJS(javascript)上总结这个JSON的课程价格 我要做两笔钱,一笔给学校,另一笔给所有学校。我已经将所有数据打印到了一张表上,但我不确定如何使用javascript函数,我想这会让您开始学习 // get numeric value and separate it by ',' var num = coursePrice.split('$')[1].split(","); // concatenate numeric string num = num[0]

我必须能够在AngularJS(javascript)上总结这个JSON的课程价格


我要做两笔钱,一笔给学校,另一笔给所有学校。我已经将所有数据打印到了一张表上,但我不确定如何使用javascript函数,我想这会让您开始学习

// get numeric value and separate it by ','
var num = coursePrice.split('$')[1].split(","); 
// concatenate numeric string
num = num[0] + num[1]; 
// turn it into a float
num = parseFloat(num);
下次不要指望别人来做你的工作:

var sum = 0;

data.forEach(function(d) {
   d.professor.forEach(function(p) {
      var num = p.coursePrice.split('$')[1].split(","); 
      // concatenate numeric string
      num = num[0] + num[1]; 
      // turn it into a float
      num = parseFloat(num);
      allSum += num;
   });
});
这将适用于所有学校,对于单个学校,您可以使用jQuery$.grep函数来完成

对于没有.grep美元和jQuery的个别学校:

var schools = [];
var schoolsSum = [];

data.forEach(function(d) {
   if(schools.indexOf(d.school) == -1) {
      schools.push(d.school);
      schoolsSum.push(0);
   }
   var schoolIndex = schools.indexOf(d.school);
   d.professor.forEach(function(p) {
      var num = p.coursePrice.split('$')[1].split(","); 
      // concatenate numeric string
      num = num[0] + num[1]; 
      // turn it into a float
      num = parseFloat(num);
      allSum += num;
      schoolsSum[schoolIndex] += num;
   });
});
在此之后,您应该有两个数组,一个包含学校名称,另一个包含schols课程价格总和。您可以通过以下两种方式轻松创建对象:

var schoolsObj = new Object();
var index = 0;
schools.forEach(function(s) {
   schoolsObj[s] =  schoolsSum[index];
   index += 1;
});

这里要解决的第一个问题是将
coursePrice
解析为一个数字,为此可以使用
String.prototype.replace()
,然后将其更改为一个数字

大概是这样的:

+coursePrice.replace(/[^0-9\.]+/g,"");
其中,
+
符号将替换的字符串更改为数字

现在让我们回到要创建的两个函数,首先让我们创建一个具有函数的服务,
getSchoolCoursePrice()
getTotalSchoolCoursePrice()

服务

SchoolService.getSchoolCoursePrice()
获取学校的课程总价格,因为您注意到该函数接受一个学校参数

另一方面,getTotalSchoolCoursePrice()接受一个学校数组的参数,该数组获取每个学校的
总课程价格。此
totalCoursePrice
是在迭代每个学校,然后使用
SchoolService.getSchoolCoursePrice()
为其分配此属性时分配的

控制器

如您所见,控制器获取学校列表,然后将
totalCoursePrice
属性附加到每个学校

HTML


学校
课程总价
加载。。。
总价

coursePrice
真的是用美元符号格式化的吗?是的,它是:S im confussei如果您可以控制后端将其更改为无格式的数字(不带美元符号和逗号),那么求和就更容易了。我无法更改JSON:Shmm,所以您想要每个学校的另一个总和数组?或者你想把总和加到数组中的每个学校项目上吗?我想到了像这样的@Teo unaControllers.contorller(“sumCoursePrice”、['$scope','unservice',function($scope,unservice){var sum=0;unservice.getAllSchools(function(error,data){if(!error){sum+=data.professors.coursePrice;}返回sum;});}]);但我不知道我是否能做好这是一个开始,但首先你需要将data.professors.coursePrice值转换为float,我可以使用jQuery帮助你开始这个问题,但你没有在问题中指定是否使用它。AngularJS是javascriptI我知道,但是你可以将AngularJS与jQuery并排使用,这个问题可以很容易地用jQuery解决…我不熟悉jQuery,但我们可以试试
  .service('SchoolService', function() {

    this.getSchoolCoursePrice = function(school) {
      return school.professor.reduce(function(value, professor) {
        return value + +professor.coursePrice.replace(/[^0-9\.]+/g,"");
      }, 0.00);
    };

    this.getTotalSchoolCoursePrice = function(schools) {
      return schools.reduce(function(value, school) {
        return value + school.totalCoursePrice;
      }, 0.00);
    };

  })
.controller('SchoolController', function($scope, $http, SchoolService) {

    $http.get('schools.json').success(function(schools) {

      $scope.schools = schools.map(function(school) {
        school.totalCoursePrice = SchoolService.getSchoolCoursePrice(school);
        return school;
      });

      $scope.totalPrice = SchoolService.getTotalSchoolCoursePrice(schools);

    });

  });
<table>
  <tr>
    <td>School</td>
    <td>Total Course Price</td>
  </tr>
  <tr ng-show="!schools">
    <td colspan="2">Loading...</td>
  </tr>
  <tr ng-repeat="school in schools track by $index">
    <td ng-bind="school.school"></td>
    <td ng-bind="school.totalCoursePrice"></td>
  </tr>
  <tr ng-show="schools">
    <td>Total Price</td>
    <td ng-bind="totalPrice"></td>
  </tr>
</table>