mysterious_guy mysterious_guy - 1 month ago 5
jQuery Question

iterations in document.ready (function) in jquery , d3.js

my data looks like this:

var IDData = JSON.stringify([["node/105173", "node/38180995", "Agent", "Customer", "1379644.0", 1, 264.0, "1374903"]...]


Every row of data follows the same format. But the length of rows of the array arrays is not fixed. I am trying to plot graphs using this data using d3.js.

$( document ).ready(function() {

console.log(IDData);
var galData = JSON.parse(IDData);
need a way here to iterate through the data so that
var startnodes = galData[0][0],galData[1][0]....galdata[n][0]
where n is the length of the array
var endnodes = galData[0][1],galData[1][1],galData[2][1]...galData[n][1]


var nodetype = galData[0][2],galData[1][2],galData[2][2]....galData[n][2]

var Paytime = galData[0][3],galData[1][3]..galdata[n][3]
var TXN_COUNT = galdata[0][4],galData[1][4],galData[2][4]...galData[n][4]
var Total_Amt = galData[0][5],galData[1][5],galData[2][5]...galData[n][5]

var SendTime = galData[0][6],galData[1][6]...galData[n][6]

makegraph(startnodes,endnodes)


var
nodetype
is a node attribute and var
Paytime
,
TXN_COUNT
,
Total_Amt
,
SendTime
are link attributes.

Not at all much familiar with d3.js and did not find a easy way to iterate .

Answer
var IDData = JSON.stringify([["node/105173", "node/38180995", "Agent", "Customer", "1379644.0", 1, 264.0, "1374903"],["node/105173", "node/38180995", "Agent", "Customer", "1379644.0", 1, 264.0, "1374903"],["node/105173", "node/38180995", "Agent", "Customer", "1379644.0", 1, 264.0, "1374903"]]);
var galData = JSON.parse(IDData);

var startnodes = [],
    endnodes = [],
    nodetype = [];

// If array
galData.map(function(e,i){
    startnodes.push(e[0]);
    endnodes.push(e[1]);
    nodetype.push(e[2]);
});


console.log(startnodes);
console.log(endnodes);
console.log(nodetype);

// If string
startnodes = startnodes.join(',');
endnodes = endnodes.join(',');
nodetype = nodetype.join(',');

console.log(startnodes);
console.log(endnodes);
console.log(nodetype);

Does't cover all but you should get the picture.