I have an array of Javascript objects as follows
a=[
{id:1, parentId:2},
{id:2, parentId:3},
{id:3, parentId:4},
{id:4, parentId:1},
{id:5, parentId:5}
];
I would like to detect in this array if any object refers to itself directly (5->5) or if refers to itself by other parents like 1->2->3->4->1
what is the best way to write it?
PS. There is another question like this one but it is not answered the question. the accepted answer is about a graph and it returns color coding, How to detect a loop in a hierarchy of javascript elements but mine is parentId
my expected behaviour is: false, 5->5 and 1->2->3->4->1 or simply false, neighter colour coding nor for graph :)