2

I am using API to build something, but it's not well documentated, so I am moving by guess. I have a 2 variables: g.nodes and g.edges, when I do:

console.log(g.nodes);
console.log(g.edges);

I got output for g.nodes as:

Object {hello: Object, test: Object, test1: Object, test2: Object, test3: Object…} 

and for g.edges as :

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

I understood that g.edges is an array, but what datatype is g.nodes?

Subin
  • 3,445
  • 1
  • 34
  • 63
Tigran
  • 1,049
  • 3
  • 15
  • 31
  • 7
    `console.log( typeof g.nodes );` – adeneo Dec 28 '13 at 16:55
  • Probably a JS object used as a dictionary that maps from some kind of node-address (represented as a string) to a node object. – Mike Samuel Dec 28 '13 at 16:56
  • console.log( typeof ...) gives: object object – Tigran Dec 28 '13 at 16:57
  • `console.log("%o : %s", g.nodes, g.nodes.constructor.name)` might help – Mike Samuel Dec 28 '13 at 16:58
  • @MikeSamuel, yes. But how to use it? When I do condole.log (g.nodes) and go inside tree, I can get: test: Object edges: Array[0] hide: function () { id: "test" layoutForceX: 0 layoutForceY: 0 layoutPosX: 7.705064028484167 layoutPosY: 8.559115292926737 point: Array[2] render: function (r, n) { shape: T show: function () { __proto__: Object .... So a lot of data, but when I do for (e in ...), I see only text. – Tigran Dec 28 '13 at 17:00

2 Answers2

2

for the first one g.nodes are simply a javascript object, but you can consider it as something like a HashMap or a key-value pair.

and the next one is an array or an array like object which could be a javascript object which all the keys are numbers, it could also be a arguments type object.

about how to go thru:

for g.nodes you can do this:

for(var key in g.nodes){
    var value = g.nodes[key];
    console.log(typeof value);
}

for arrays you'd better not use the first iteration method, because other than the actual data of the array it can also iterates all other properties defined in Array.prototype, for instance if you add this:

Array.prototype.myarray_prop = 1;

if you use for(var key in obj) method, in addition to the actual array values the myarray_prop will show up as a key, as far as when we usually use arrays we want to iterate the actual values of the array, not these kinds of extra props, it is not a good idea to use for(var key in obj);

so for g.edges you'd better do this:

for(var i=0;i<g.edges.length;i++){
    var edge = g.edges[i];
    console.log(typeof edge);
}
Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
  • Can you say, how to access childs? forEach fail as this is not array, for (e in g.nodes) gives only text. – Tigran Dec 28 '13 at 17:02
  • @Tigran: http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object – Felix Kling Dec 28 '13 at 17:05
-2

This is an interesting Question. Lets discuss this one by one:

console.log(g.nodes);

The output suggests that this is an Object. The properties of this Object are:

  1. hello
  2. test
  3. test1
  4. and so on...

Secondly there is an array of an object. This is essentially a JavaScript collection of objects.

A good way not to encounter such things is by debugging your Scripts properly.

A good way to debug JavaScript:

Whenever you feel like that you are not understanding the flow of JavaScript, run that script in FireFox, FireBug and insert break points at all important locations. Then FireBug will show you the type and state of each variable.

An Example of Debugging:

function SayHello(){
debugger;
var msg = "Hello World";

console.log(msg);

}

The code will stop at debugger and you can see the steps from there. You can even see the type of variable "msg".

Hope this helps you a long way.

Talha Masood
  • 993
  • 1
  • 7
  • 22