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?
$scope.applyor using angular helpers such as angular.foreachforEach