I'm trying to order an object with some fields order rule written in an array:
This is the order rules:
const fieldsOrder = [
'agentID',
'agentSequence',
'token'
];
This is the object that it needs to be ordered:
const request = {
token: 'TEST',
agentSequence: true,
agentID: '40208697',
}
So my idea was to do this:
const feeLookupRequest = {};
for(const field of fieldsOrder) {
if (request[field]) {
feeLookupRequest[field] = request[field]; // ERROR
}
}
And it works, and I can make a console.log(feeLookupRequest)
which shows:
{
agentID: '40208697',
agentSequence: true,
token: 'TEST'
}
But I'm getting some types errors that I don't understand very well:
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used
to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'.
Any hint?
EDIT: actually, if I sent agentSequence: false
the feeLookupRequest
object has not the agentSequence
property, I don't know how could I manage that.
Code for testing: