So right now I have an object with the following shape and the task is to loop over every item in all of the arrays and compare each one to all of the other items in all of the arrays.
things: Things = {
"Type1Things": [],
"Type2Things": [],
"Type3Things": [],
}
Here's how I currently have it written:
Object.keys(things).forEach( (key: string) => {
things[key].forEach( ( outerThing: Overlay ) => {
Object.keys(things).forEach( (key1: string) => {
things[key1].forEach( ( innerThing: Overlay ) => {
// compare inner and outer things
})
})
})
})
Is there a better method to perform this task, or at least a more readable way of writing it? Could a recursive function better accomplish this?
I've abstracted this out using Typescript. Might not be exactly what you're looking for but it'll help with readability, and errors that popup when grabbing the keys from objects. Also saves you having to write most of the typings in your example.
const OBJ = {
Keys: <T extends Record<string, unknown>>(obj: T): (keyof T)[] =>
(Object.keys(obj) as any)
}
Usage:
for (const keyX in OBJ.Keys(things)) {
for (const keyY in OBJ.Keys(things[keyX])) {
console.log(`${keyY}: ${things[keyX][keyY]}`);
}