JSFiddle - React, Tailwind, and code Playground
HTML
{<span id='output'></span>}
JavaScript
function Node(id, children){
this.id = id;
this.children = children || [];
this.countParents = function(nodeList){
var count = 0;
for(var i=0;i<nodeList.length;i++){
if(nodeList.children.indexOf(this.id) > -1)
count++;
}
return count;
};
};
function NodeList(nodes){
this.list = nodes || [];
this.parentCounts = function(){
var output = [];
for(var i=0;i<this.list.length;i++){
var count = 0;
for(var e=0;e<this.list.length;e++){
if(this.list[e].children.indexOf(this.list[i].id) > -1)
count++;
}
output.push(this.list[i].id+":"+count);
}
return output.join(", ");
}
};
var nodes = new NodeList([
new Node(11, [12]),
new Node(12, [13,14]),
new Node(13, [15]),
new Node(14, [15]),
new Node(15, [16,17,18]),
new Node(16),
new Node(17),
new Node(18, [19]),
new Node(19)
]);
document.getElementById("output").innerHTML = nodes.parentCounts();