0

Hi I have an array that hold the following numbers, however when I loop though the eachNode function(which iterates 13 times) it repeats all the list elements 13 times. I tested everything but it still produces an error, I'm I executing the for loop correctly?

list[61,67,78]
var len = list.length;

fd.graph.eachNode(function (node) { // loops thru all node id's in graph (13)
    for (var i = 0; i < len; ++i) {
        if (i in list) {
            var nody = list[i]; // I put the number in a variable
            var nodess = fd.graph.getNode(nody); //this takes the number and matches it with a node id, it "odjectify" it
            if (node.id != nodess.id) { //  if the list nodes are not the same
                node.setData('alpha', 0); //
                node.eachAdjacency(function (adj) { // this make the unmatched nodes disappear
                    adj.setData('alpha', 0, 'end');
                });
            }
        }
    }
});
2
  • 1
    What are you trying to do with if ( i in list )? Commented Apr 20, 2013 at 23:39
  • It's the insignificant I, used for indexing Commented Apr 20, 2013 at 23:40

2 Answers 2

1

This line is unneeded:

if (i in list)

The in keyword returns true if its right operand contains the property specified by its left operand. When using this with arrays, it returns unexpected results. The behavior of this keyword is insignificant in this context, so you should simply take it out.

Moreover, you need to create the list array like this:

var list = [61, 67, 78];

...however, when I loop though eachNode (which iterates 13 times) it repeats all the list elements 13 times

It doesn't, it in fact iterates over eachNode 13 times. You also made a for loop which will traverse the list array by its length.


Now that you've given me more detail as to what you want, here is the updated code. I hope it works for you:

fd.graph.eachNode(function (node) {
    var flag = false;

    for (var i = 0; i < len; ++i)
    {
        var nody = list[i];
        var nodess = fd.graph.getNode(nody);

        if (node.id == nodess.id) {
            flag = true; break;
        }
    }

    if (flag)
    {
        node.setData('alpha', 0);
        node.eachAdjacency(function (adj) {
           adj.setData('alpha', 0, 'end');
        });
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

I need to "objectify" the elements in the list array using - fd.graph.getNode() method- then I have to compare them to each node in the graph, however I cant seem to figure out how to nest the loop in the eachnode array in order to test each element node to each nodes.
If I loop outside then the variable indexing the elements will just give me the last one in the array
@Squirtle Set a boolean value outside the for loop. Then inside the if block if (node.id != nodess.id) set the variable to true then break. Then outside the for loop check if that boolean is true. If it is, then run the original code that was inside that if block.
@Squirtle I edited my code at the bottom. Does it help? If not, what does it do wrong?
thanks for the code, however it made all the nodes disappear! I'm thinking the array loop somehow messes up the testing of the nodes
|
0

This is the behavior by design:

You loop over the graph (13 times as you say), then inside each iteration you loop over your array (3 items).

If you only want to loop once over your array, just move it out of the outer loop

1 Comment

The reason I have to loop inside is because I want the list elements to be objects and then tested against the others, if I loop outside then I would not be able to test each list.node object to the other one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.