0

I have 2 arrays: "nodes" and "edges"

{
"nodes":[
    {"id": "MPLS-001-BH01", "label": "001-BH01" , "Host_IP": "10.158.20.3" , "size": "10.0" , "r": "153" , "g": "153" , "b": "153" , "x": "521" , "y": "104" },
    {"id": "MPLS-002-BH01", "label": "002-BH01" , "Host_IP": "10.158.20.6" , "size": "10.0" , "r": "153" , "g": "153" , "b": "153" , "x": "336" , "y": "315" },
    {"id": "MPLS-003-BH01", "label": "003-BH01" , "Host_IP": "10.158.20.4" , "size": "10.0" , "r": "153" , "g": "153" , "b": "153" , "x": "708" , "y": "134" },
    {"id": "MPLS-004-BH01", "label": "004-BH01" , "Host_IP": "10.158.20.5" , "size": "10.0" , "r": "153" , "g": "153" , "b": "153" , "x": "464" , "y": "314" },
    {"id": "MPLS-MSC-BH01", "label": "MSC-BH01" , "Host_IP": "10.158.20.1" , "size": "10.0" , "r": "153" , "g": "153" , "b": "153" , "x": "271" , "y": "223" },
    {"id": "MPLS-MSC-BH02", "label": "MSC-BH02" , "Host_IP": "10.158.20.2" , "size": "10.0" , "r": "153" , "g": "153" , "b": "153" , "x": "380" , "y": "110" }
    ],
"edges":[
    {"source": "MPLS-002-BH01", "target": "MPLS-004-BH01", "weight": "1.0" , "Host_A": "MPLS-002-BH01" , "Interface_A": "GigabitEthernet1/2/0" , "Host_B": "MPLS-004-BH01" , "Interface_B": "GigabitEthernet1/2/1" , "BW": "100" },
    {"source": "MPLS-003-BH01", "target": "MPLS-004-BH01", "weight": "1.0" , "Host_A": "MPLS-003-BH01" , "Interface_A": "GigabitEthernet1/2/1" , "Host_B": "MPLS-004-BH01" , "Interface_B": "GigabitEthernet1/2/0" , "BW": "100" },
    {"source": "MPLS-MSC-BH01", "target": "MPLS-MSC-BH02", "weight": "1.0" , "Host_A": "MPLS-MSC-BH01" , "Interface_A": "GigabitEthernet2/0/0" , "Host_B": "MPLS-MSC-BH02" , "Interface_B": "GigabitEthernet2/0/0" , "BW": "100" }  
    ]

}

to draw a lines, I need to get x,y coordinates of nodes by 'source' and 'target' params of edge elements, so I wrote a function:

function get_node_x(search_key)
{

    for (var i = 0; i < nodes_list.length; i++) 
    {
        if(nodes_list[i].id = search_key)
            {
            return nodes_list[i].x;
            }
    }

};

it seems quite simple, BUT, then I test this :

links_list.forEach(function(d){
console.log(d.source+" ; "+get_node_x(d.source))
});

I have only [0].x coordinate:

MPLS-002-BH01 ; 521 
MPLS-003-BH01 ; 521 
MPLS-MSC-BH01 ; 521 

I don't get it....

Dimas51
  • 19
  • 3
  • `nodes_list[i].id = search_key` should be `nodes_list[i].id === search_key`. You have to use comparison, not assignment. Read these articles to [learn how to](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) [**debug** JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging), so you can provider more useful context information (and help yourself). – Felix Kling Jul 06 '14 at 07:49
  • I found solution in this [topic](http://stackoverflow.com/questions/2167602/optimum-way-to-compare-strings-in-javascript), by Daniel Vassalo. It's working! – Dimas51 Jul 06 '14 at 09:27

0 Answers0