I have this function that sort some input object based on an array called keys;
const keys = ["token", "agentID", "agentSequence", "allOptions"]
function sortRequest(request) {
return keys.reduce((sortedRequest, key) => {
if (key in request) {
sortedRequest[key] = request[key]
}
return sortedRequest
}, {})
}
console.log(sortRequest({
allOptions: false,
agentSequence: 6,
agentID: 123,
token: 'test',
notVisible: true
}));
The only problem is if some value of the input is not present on the array it will be lost on the returned object. I'm trying to fix this issue, but couldn't get it. The idea is to get each property of the object.
keysarrays. In that arraytokenis first, and so on. I need to send this object sorted with the way that array is sorted. I know that objects sorting is a very large issue, with many scopes where may it fails, but this object will be sent to an external SOAP api made in 1999 with a lot of history behind... I can't control what receives that API (it has many if inside a for, etc...).