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.