angularjs - Making Graph in Javascript - Issue -
i need debugging problem simple graph (data structure, not chart). can insert node, create path next nodes fine; that's not problem. problem when call returngraph
node @ first index returned [object object]
, other nodes returned should be. being wrriten angularjs way.
so looked solution , read in few places has either object's built in string method or conflict between javascript-objects , json-objects. unfortunately i'm little lost since i'm not familiar javascript internals.
first off, here example json object using:
//json object waiting passed node() obj1 = { id: 6111, name: "node1", stats: { "speed": 4 }, ranks: 5, prereq: 0 }
here rest of code:
//pass data json object through constructor create node function node(data){ this.name = data.name; this.stats = data.stats; this.maxrank = data.ranks; this.rank = 0; } //return node name node.prototype.returnname = function(){ return this.name; } //increase node rank node.prototype.increaserank = function(){ if (this.rank < this.maxrank){ this.rank += 1; } console.log(this.rank); } //graph object function graph(){ this.edges = [] } graph.prototype.returnneighbors = function(node){ return this.edges[node]; } graph.prototype.addneighbor = function(node1, node2, node3, node4){ // prints out node1 object console.log(node1); // prints out node1 object if (node1 == null){ console.log("something went terribly wrong!"); } else if (node1 != null && node2 == null && node3 == null && node4 == null) { this.edges.push({[node1]: []}); } else if (node4 == null){ this.edges.push({[node1]:[node2, node3]}) } else{ this.edges.push({[node1]: [node2, node3, node4]}) } } graph.prototype.returngraph = function(){ return json.stringify(this.edges, null, 2); } // create node each json object $scope.node1 = new node(obj1); $scope.node2 = new node(obj2); $scope.node3 = new node(obj3); $scope.node4 = new node(obj4); // create graph var g = new graph(); g.addneighbor($scope.node1, $scope.node2, $scope.node3, $scope.node4); $scope.g = g; console.log(g.returngraph());
this problem lies , above statement prints out. keep in mind graph
:
[ { "[object object]": [ { "name": "node2", "stats": { "speed": 4 }, "maxrank": 5, "rank": 0 }, { "name": "node3", "stats": { "attack": 4 }, "maxrank": 5, "rank": 0 }, { "name": "node4", "stats": { "block": 4 }, "maxrank": 5, "rank": 0 } ] } ]
i've tried using json.parse()
wasn't working. converted first node string json.stringify()
, directly input string data instead of actual object, got me closer , yet full of escapes (\\
,\t
,\n
). using regex didn't clean either.
so there better way solve [object object]
problem? looked through lot of answers not find solution. help!
i suggest update addneighbor()
function this:
graph.prototype.addneighbor = function(node1, node2, node3, node4){ // prints out node1 object console.log(node1); // prints out node1 object if (node1 == null){ console.log("something went terribly wrong!"); } else if (node1 != null && node2 == null && node3 == null && node4 == null) { this.edges.push({[node1.name]: []}); } else if (node4 == null){ this.edges.push({[node1.name]:[node2, node3]}) } else{ this.edges.push({[node1.name]: [node2, node3, node4]}) } }
you're pushing new object existing object. new object has key defined node1.name
give actual string value key name. code using node1
. javascript realize node1 isn't string automatically convert object string you're getting [object object]
from.
Comments
Post a Comment