JSFiddle - React, Tailwind, and code Playground

HTML

Expected out put


{
11:0,
12:1,
13:1,
14:1,
15:2,
16:1,
17:1,
18:1
19:1
}

JavaScript

var node=[
  {
   
    "node_from": 11,
    "children": [
      {
        "node_to": 12
      }
    ]
  },
  {
   
    "node_from": 12,
    "children": [
      {
        "node_to": 13
      },
      {
        "node_to": 14
      }
    ]
  },
  {
    "node_from": 13,
    "children": [
      {
        "node_to": 15
      }
    ]
  },
  {
    
    "node_from": 14,
    "children": [
      {
        "node_to": 15
      }
    ]
  },
  {
   
    "node_from": 15,
    "children": [
      {
        "node_to": 16
      },
      {
        "node_to": 17
      },
      {
        "node_to": 18
      }
    ]
  },
  {
    
    "node_from": 16,
    "children": [
      
    ]
  },
  {
    "node_from": 17,
    "children": [
      
    ]
  },
  {
    
    "node_from": 18,
    "children": [
      {
        "node_to": 19
      }
    ]
  },
  {
    
    "node_from": 19,
    "children": [
      
    ]
  }
]



var obj={}
for(var i=0;i<node.length;i++){

console.log(node[i]);
var node=node[i]
if(obj.hasOwnProperty(node.node_from)){
obj[node_from]=obj[node_from]++;
}else{
obj[node_from]=0;
}

}
console.log(obj)