Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created November 13, 2019 03:15
Show Gist options
  • Save justinyoo/9fe349aed14085321eaf48b14338dc9b to your computer and use it in GitHub Desktop.
Save justinyoo/9fe349aed14085321eaf48b14338dc9b to your computer and use it in GitHub Desktop.
Getting the Latest Array Item with Inline Script in Logic App
"use strict";
// Assign the array value from the output of the previous action, `List Backups`.
var items = workflowContext.actions.List_Backups.outputs.body.value;
var sorted = items.sort(function (a, b) {
var dateA = a.Name.replace('.json', '');
var dateB = b.Name.replace('.json', '');
if (dateA > dateB) {
return -1;
}
if (dateA < dateB) {
return 1;
}
return 0;
});
var result = sorted[0].Path;
// Returns the result as output.
return result;
"use strict";
var items = [
{
"Name": "20191101.json",
"Path": "/path/20191101.json"
},
{
"Name": "20191102.json",
"Path": "/path/20191102.json"
},
{
"Name": "20191103.json",
"Path": "/path/20191103.json"
},
{
"Name": "20191104.json",
"Path": "/path/20191104.json"
}
];
var sorted = items.sort(function (a, b) {
var dateA = a.Name.replace('.json', '');
var dateB = b.Name.replace('.json', '');
// dateA is later than dateB: dateA gets the lower index.
if (dateA > dateB) {
return -1;
}
// dateA is older than dateB: dateB gets the lower index.
if (dateA < dateB) {
return 1;
}
// dateA and dateB is the same
return 0;
});
var result = sorted[0].Path;
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment