Reputation: 8225
I have array I'm getting from calling a .net controller.
I'm getting these values for dates:
/Date(1445256000000)/
and /Date(1445256900000)/
Instead of that I want to be able to get proper date values.
Now since I have array of objects I want to be able to update them in the array before sending them to the view.
This is what I have:
$http({
method: 'GET',
url: '/Driver/GetDriverTrips',
params: { id: id }
}).
success(function (data) {
var startDate= new Date(data[0].StartTime.match(/\d+/)[0] * 1);
alert(myDate);
});
So this conversion works properly but the thing is that I want to loop through all the same objects in the array, do the conversion and then again save it in the array.
Is there any function I can do that?
Upvotes: 1
Views: 1379
Reputation: 9458
Something like
$.each(data, function(index, element) {
var startDate= new Date(element.StartTime.match(/\d+/)[0] * 1);
});
would loop through all of the returned elements and extract the date.
You can have a second array, say finalDates
and push the converted dates into that array as you loop.
Upvotes: 0
Reputation: 15154
try using map
var array = $(data).map(function(){
return new Date(this.StartTime.match(/\d+/)[0] * 1);;
});
Or, to overwrite StartTime
$.each(data, function(index, element) {
element.StartTime = new Date(element.StartTime.match(/\d+/)[0] * 1);
});
Upvotes: 1
Reputation: 1516
My method uses a usual for
statement, which is a little more verbose, but I prefer, as long is the most efficient way to traverse an array.
So, for each iteration, I got data
at position i
(data[i]
), and stored the result in a temporary variable, called finalDate, and then finally push it to finalDates array. That's what you are looking for?
var finalDates = [];
$http({
method: 'GET',
url: '/Driver/GetDriverTrips',
params: { id: id }
}).
success(function (data) {
var finalDate;
for(var i=0; i<data.length; i++) {
finalDate = new Date(data[i].StartTime.match(/\d+/)[i] * 1);
finalDates.push(finalDate);
}
});
Upvotes: 0