我怎样才能优雅地更新哈希数组中的值

How could I elegantly update the value inside array of hashes

本文关键字:哈希 更新 数组      更新时间:2023-09-26

目前我正在使用 $.each 来更新位于哈希数组中的值。

我想知道是否有任何优雅的解决方案来实现这一目标。

使用Date.parse更新每个 X 值

  $.each($scope.flights, function() {
      var current_flight_no =  this
      $.each(current_flight_no.data, function(){
        this.x = Date.parse(this.x);
      })
  });

$scope.航班

    [{
            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
            }]
        }
    ]

我会在这里使用纯JavaScript,即

$scope.flights.forEach(function(flight) {
  flight.data.forEach(function(item) {
    item.x = Date.parse(item.x);
  });
});

您可以使用"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);
    }
}