2

I have an array of objects with a date formatted in MMMM Do YYYY format. I need to convert this into a UNIX timestamp to arrange them and then convert them back into the readable date format.

However, in doing this. It seems that my changes from within the forEach callback are not applied to the $scope.lalala variable.

My code:

    function compare(a, b) {
  if (a.date < b.date)
    return -1;
  if (a.date > b.date)
    return 1;
  return 0;
}
$scope.lalala = arrayofincompleteorders;
$scope.lalala.forEach(function(hiVanilla, index) {
  hiVanilla.date = moment(hiVanilla.date, 'MMMM Do YYYY').format('x');
  if (index == $scope.lalala.length - 1) {
    $scope.lalala.sort(compare); timestamps as expected
    console.log($scope.lalala); //logs the date property with unix 
    callback();
  }
});
console.log($scope.lalala); //logs the date property with unix timestamps, why?

function callback() {
  $scope.lalala.forEach(function(order, index) {
    console.log(order.date); //unix timestamp
    $scope.lalala[index].date = moment(order.date, 'x').format('MMMM Do YYYY');
    console.log($scope.lalala[index].date); //formatted timestamp
  });
};

Edit: I have the same problem even with the angular.forEach loop in the callback:

function callback(){
    angular.forEach($scope.lalala, function(value, key) {
    console.log(value.date);
  value.date = moment(value.date, 'x').format('MMMM Do YYYY');
  console.log($scope.lalala[key].date);
});
  console.log("fire!");
  $scope.apply();
};

I get the dates to change successfully but then it says that $scope.apply() is not a function which borks the rest of my script.

Edit2:

I got rid of the callback and have everything in one angular.forEach but it still doesn't apply?

$scope.lalala = arrayofincompleteorders;
 angular.forEach($scope.lalala, function(hiVanilla, key) {
  hiVanilla.date = moment(hiVanilla.date, 'MMMM Do YYYY').format('x');
  if (key == $scope.lalala.length - 1) {
    $scope.lalala.sort(compare); //timestamps as expected
    console.log($scope.lalala); //logs the date property with unix 
    console.log(hiVanilla.date); //unix timestamp
    hiVanilla.date = moment(hiVanilla.date, 'x').format('MMMM Do YYYY');
    console.log($scope.lalala[key].date); //formatted timestamp
  }
});
console.log($scope.lalala); //logs the date property with unix timestamps, why?
8
  • Possible duplicate of foreach loop in angularjs Commented Aug 19, 2016 at 17:54
  • @AhmadBamieh that looks like a different (angular specific) function... is that what I should be using? Commented Aug 19, 2016 at 17:55
  • yes, you need to update the scope to reflect the changes, you can do this manually by calling $scope.apply or using angular helpers such as angular.foreach Commented Aug 19, 2016 at 17:57
  • @AhmadBamieh I get $scope.apply() is not a function and it seems necessary even when using the angular forEach Commented Aug 19, 2016 at 18:11
  • @AhmadBamieh when I change to $scope.$apply() I get a digest already in progress error! :( Commented Aug 19, 2016 at 18:45

1 Answer 1

0

It looks like I was using angular.forEach in a way that it was not designed.

The following worked, basically I just assigned it by pushing it into an empty array rather than trying to alter the array from which I was looping inside of:

        $scope.lalala=[];
        var log = = arrayofincompleteorders;
        angular.forEach(log, function(value, key) {
        if(value.complete!="TRUE")
        {
        i++;
        value.date = moment(value.date, 'x').format('MMMM Do YYYY');
        this.push(value); //put the new value in $scope.lalala as specified below
        }
        }, $scope.lalala);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.