user944513 February 2016

how to counts parent in an javascript object (getting not defined error)

I am trying to make a object which could the number of parents of element . I have a array of objects .in which there is two field to and from .I want to calculate the length of parents here is my code ?

Example

  • "node_from": 11, don't have parent ..it don't have to field .so it length is 0,zero;
  • "node_from": 12, is coming from 11 .so it have parent .so it parent length is one or 1 and same as in 13 , 14,16,18,19.
  • "node_from": 15, has 2 parent coming from 13 and 14 ..so it length is 2 .

can we find this

here is my code

https://jsfiddle.net/ood2ezvz/2/

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)

It is giving error ?

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

Answers


Dustin Poissant February 2016

Assuming "node_from" is the ID of the current node, and "node_to" is the children each node points to. Here is a function which can count the children of each node.

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": [
      
    ]
  }
];

function countParents(nodes, num){
  var count = 0;
  for(var i=0;i<nodes.length;i++){
  	if(nodes[i].children){
    	for(var e=0;e<nodes[i].children.length;e++){
        if(nodes[i].children[e].node_to == num)
          count++;
      }
    }
  }
  return count;
}

function countEachParents(nodes){
  var output = [];
  for(var i=0;i<nodes.length;i++){
    output.push( nodes[i].node_from + ":" + countParents(nodes, nodes[i].node_from) );
  }
  return output;
}

document.getElementById( "output" ).innerHTML = countEachParents(node).join(", ");
{<span id='output'></sp 


DataHerder February 2016

I have fixed your code with this jsfiddle.

https://jsfiddle.net/ood2ezvz/8/

Essentially you were using hasOwnProperty incorrectly in this context. What appears to work well is undefined. The code below gets the exact output that you are looking for. Count how many node_to are in the children and set the original node_from to 0.

var obj = {};

for (var i = 0; i < node.length; i++) {
    var new_node = node[i];
  console.log(new_node);
  if (typeof obj[new_node.node_from] === 'undefined') {
    obj[new_node.node_from] = 0;
  }
  for (var j = 0; j < new_node.children.length; j++) {
    if (typeof obj[new_node.children[j].node_to] !== 'undefined') {
        obj[new_node.children[j].node_to]++;
    }
  }
}
console.log(obj);


Nina Scholz February 2016

This proposal uses an temporary object for the parents and returns then the length of every parent.

function getParentCount(nodes) {
    var parent = {}, o = {};
    nodes.forEach(function (n) {
        parent[n.node_from] = parent[n.node_from] || [];
        n.children.forEach(function (a) {
            parent[a.node_to] = parent[a.node_to] || [];
            parent[a.node_to].push(n.node_from);
        });
    });
    Object.keys(parent).forEach(function (k) { o[k] = parent[k].length; });
    return o;
}

var nodes = [{ "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": [] }];

document.write('<pre>' + JSON.stringify(getParentCount(nodes), 0, 4) + '</pre>');


BenG February 2016

you need to create a recursive function like so:-

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": [

  ]
}]

function recursiveSearch(node_from, count) {
  for (var i = 0; i < node.length; i++) {
    var current = node[i];
    for (var j = 0; j < current.children.length; j++) {
      var child = current.children[j];
      if (node_from == child.node_to)
        recursiveSearch(current.node_from, ++count);
    }
  }
  return count;
}

var obj = {}
for (var i = 0; i < node.length; i++) {
  var current = node[i]
  if (current.hasOwnProperty('node_from')) {
    obj[current.node_from] = recursiveSearch(current.node_from, 0);
  }
}

document.write('<pre>' + JSON.stringify(obj) + '</pre>')
console.log(obj)


Nijeesh February 2016

Try this method

var obj={}
node.forEach(function(object){
    if(!obj[object.node_from])
      obj[object.node_from] = 0;
    object.children.forEach(function(child){
        if(!obj[child.node_to])
          obj[child.node_to] = 0;
        obj[child.node_to]++;
    })
})
console.log(obj);

Post Status

Asked in February 2016
Viewed 3,596 times
Voted 8
Answered 5 times

Search




Leave an answer