I have a situation where I have an object where some of the properties contain arrays like so:
current: {
primary: [ [Object] ],
secondary: [ [Object] ],
tertiary: [],
quaternary: [],
quinary: [],
senary: [],
septenary: [],
octonary: [],
nonary: [],
denary: []
},
To find the correct one, I could do something not very elegant, like this:
getLastActive = (current) => {
let lastActive;
if(current.primary[0] && current.primary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.primary[0];
} else if(current.secondary[0] && current.secondary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.secondary[0];
} else if(current.tertiary[0] && current.tertiary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.tertiary[0];
} else if(current.quaternary[0] && current.quaternary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.quaternary[0];
} else if(current.senary[0] && current.senary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.senary[0];
} else if(current.septenary[0] && current.septenary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.septenary[0];
} else if(current.octonary[0] && current.octonary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.octonary[0];
} else if(current.nonary[0] && current.nonary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.nonary[0];
} else if(current.denary[0] && current.denary[0].coverage.purchaser.name !== 'customer') {
lastActive = current.denary[0];
}
return lastActive;
}
const enums = {
ORDER: {
1: "primary",
2: "secondary",
3: "tertiary",
4: "quaternary",
5: "quinary",
6: "senary",
7: "septenary",
8: "octonary",
9: "nonary",
10: "denary",
},
};
While that works, what I'd prefer to do is something more succinct like this:
getLastActive = (current) => {
let lastActive;
let sequence = enums.ORDER['some value']; // Not sure what goes here
if(current[enums.ORDER[sequence]][0].coverage.purchaser.name !== 'customer') {
lastActiveCommercial = current[enums.ORDER[sequence]][0];
}
return lastActive;
}
I've seen this done elsewhere. However, what I'm unclear on is what I can put in this line, to get the right array reference:
let sequence = enums.ORDER['some value']; // Not sure what goes here
To be clear, I know that the data I want is the first property of primary, secondary, tertiary, etc., that is not an empty array, and where coverage.purchaser.name !== 'customer'.
What can I use to pull this out based on the data above?