如何使用angularjs中的map函数和jQuery中的$ map相同?

时间:2015-07-28 06:15:33

标签: javascript angularjs

我正在将jQuery代码转换为angaularjs代码,在某些地方他们使用jQuery中使用的map函数,所以我想在angularjs代码中构建相同的功能。请参阅以下代码。还有一个这个self.series是json的回应。

alert("before map function"+JSON.stringify(self.series));   
      $.map(self.series, function(serie) {
        serie.color = last_point_color = self.color(serie.name);
        $.map(serie.data, function(statistic) {
          // need to parse each date'
          //alert("x"+statistic.x);
          statistic.x = d3.time.format.utc('%Y-%m-%dT%H:%M:%S').parse(statistic.x);
          //alert("statistic_x="+statistic.x);
          statistic.x = statistic.x.getTime() / 1000;
          //alert("statistic_x/1000="+statistic.x);
          last_point = statistic;
          //alert("last_point="+last_point.x);  
          // last_point=1437541894
          last_point.color = serie.color;
        });
      });
      alert("After map function"+JSON.stringify(self.series));   

警报在调用地图之前

          before map function[{"meter":"instance","data":[
          {"y":1,"x":"2015-07-22T05:01:33"}, {"y":1,"x":"2015-07-23T05:01:34"},
          {"y":1,"x":"2015-07-24T05:01:34"}, {"y":1,"x":"2015-07-25T05:03:39"},
          {"y":1,"x":"2015-07-26T02:43:39"}, {"y":1,"x":"2015-07-28T04:58:54"}],
          "name":"demo","unit":"instance"}]

调用地图后

          After map function[{"meter":"instance","data":[{"y":1,"x":1437541293,"color":"#1f77b4"},
          {"y":1,"x":1437627694,"color":"#1f77b4"},{"y":1,"x":1437714094,"color":"#1f77b4"},
          {"y":1,"x":1437800619,"color":"#1f77b4"},{"y":1,"x":1437878619,"color":"#1f77b4"},
          {"y":1,"x":1438059534,"color":"#1f77b4"}],"name":"demo","unit":"instance","color":"#1f77b4"}]

在这里你可以看到地图转换x的值,这里是x代表大纪元时间。稍后我将绘制这些数据图。 所以我想在angularjs中使用相同的功能,请帮助我。

我希望在jQuery中使用相同逻辑的Angularjs代码

app.controller('ceilometerCtrl', function($scope, $http) {
  $http.get("http://192.168.206.133:8080/admin/metering)
  .success(function(response) {

      if(response.series.length <=0){
          alert('No Data is available');
      }
      else{

           $scope.metrics=response.series[0].data;
          /* Here same logic as jquery $map */
      }

  });
});

1 个答案:

答案 0 :(得分:0)

这里没有特定的角度js。 你可以简单地使用javascript的地图功能

self.series = self.series.map(function(s){
 return s.color = 'whatever you want to assign here';
});