目前我正在使用$ .each来更新位于散列数组中的值。
我想知道是否有任何优雅的解决方案来实现这一目标。
Date.parse
$.each($scope.flights, function() {
var current_flight_no = this
$.each(current_flight_no.data, function(){
this.x = Date.parse(this.x);
})
});
[{
name: "JW100",
data: [{
name: "Vanilla",
x: "2016-03-15",
y: 3888
}, {
name: "Vanilla",
x: "2016-03-21",
y: 9048
}, {
name: "Vanilla",
x: "2016-03-22",
y: 7008
}]
}, {
name: "GK12",
data: [{
name: "Jetstar",
x: "2016-03-15",
y: 3678
}, {
name: "Jetstar",
x: "2016-03-20",
y: 4478
}, {
name: "Jetstar",
x: "2016-03-22",
y: 6378
}]
}
]
答案 0 :(得分:0)
我会在这里使用纯JavaScript,即
$scope.flights.forEach(function(flight) {
flight.data.forEach(function(item) {
item.x = Date.parse(item.x);
});
});
答案 1 :(得分:0)
您可以使用“for”,例如:
for(var x in $scope.flights){
var data = $scope.flights[x].data
for(var i in data){
var flight = data[i];
var dateString = flight.x;
flight.x = Date.parse(dateString);
}
}