I am currently in the process of migrating jQuery code to AngularJS. I have encountered some instances where the map function is used in jQuery, and I need to replicate the same functionality in AngularJS. Below is the code snippet that demonstrates this. Additionally, the variable self.series contains a JSON response.
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));
Alert Before calling map
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 calling Map
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"}]
The map function transforms the value of x, where x represents Epoch time. This data will later be used to draw graphs. I am seeking to implement the same functionality in AngularJS - any assistance with this would be greatly appreciated.
AngularJS code where I aim to apply the logic similar to jQuery
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 */
}
});
});