I must show a set of images that depend on each other. For example
Image A depends on no one
Image B depends on A
Image C depends on A and B
Image D depends on F
Image E depends on D and C
Image F depends on no one
I have a javascript object like this:
const imageDependencies = {
A: [],
B: ['A'],
C: ['A', 'B'],
D: ['F'],
E: ['D', 'C'],
F: []
}
I need to get all my image names ordered by their dependencies. The result of this example could be any of these:
// so first yo get the value of A. Once you have it you can get the value of B. Once you have the value of A and B you can get C, and so on
result_1 = [A, B, C, F, D, E]
// this could be another correct result
result_2 = [A, F, D, B, C, E]
I've tried using the Array.sort()
function like this:
let names = Object.keys(imageDependencies);
names.sort((a,b) => {
if(imageDependencies [a].includes(b)) return 1
else return -1
})
But is not working properly.
How can this be done?