I am a bit stuck. First off, I know there are similar questions, and I have tried various different options, but I am simply missing something. I have not had to do this type of method before, so I would appreciate your help and kindness.
I know I should use (I think) Object.keys
and .map
, and a function that looks something like this:
function comp(a, b) {
return new Date(a.result.date).getTime() - new Date(b.result.date).getTime();
}
but I am struggling to get it to work.
I have tried:
var uptime = $scope.frequencyData.production_deploys;
var myObjects = Object.keys(uptime).map(function (itm) {
return uptime[itm];
});
var arr = [];
for (key in uptime) {
uptime[key].time = key;
arr.push(uptime[key]);
}
arr.sort(function (a, b) {
return new Date(a.time) - new Date(b.time);
});
But this created an array
with no dates.
I have tried:
uptime = Object.keys($scope.frequencyData.production_deploys);
But this created an array
with only dates
So here is my Question:
STEP 1: How can I sort an object
of objects
({{}, {}, {}} (JSON) according to the date parameter
, and then
STEP 2: Find the last date where the object
has a parameter
of success
STEP 3: And then assign this to a var
?
This is my JSON data (object
of objects
):
{
"production_deploys": {
"2017-07-08": {
"failures": 2,
"success": 1
},
"2017-07-09": {
"failures": 2,
"success": 1
},
"2017-07-10": {
"failures": 2,
"success": 3
},
"2017-07-11": {
"failures": 2,
"success": 10
},
"2017-07-12": {
"failures": 2,
"success": 2
},
"2017-07-13": {
"failures": 2,
"success": 2
},
"2017-07-14": {
"failures": 2,
"success": 1
},
"2017-07-15": {
"failures": 2,
"success": 1
},
"2017-07-16": {
"failures": 2,
"success": 1
},
"2017-07-17": {
"failures": 2,
"success": 3
},
"2017-07-18": {
"failures": 2,
"success": 10
}
},
"pull_requests": {
},
"test_deploys": {
},
"uptime": {
}
}
The data I would like to sort after retrieving the data, would be $scope.frequencyData.production_deploys
.
Desired output would be: $scope.lastScuccessfulProductionDeploy = the correct object.date;
Some refs I have looked at: