Question

Hello I have the following Javascript code where I try to convert the object obtained from Neo4J database into a nice array or JSON (I'll be able to deal with one of the two) for further use with Gephi / Sigma.

But it doesn't work...

Any idea why?

var myObj = [
    [ 'b7145841-962f-11e3-8b8e-abca0f9fdedd',
        'painquotidien',
        'b7145842-962f-11e3-8b8e-abca0f9fdedd',
        'cafeamour',
        'b7145843-962f-11e3-8b8e-abca0f9fdedd' ],
    [ 'cce97c91-962f-11e3-8b8e-abca0f9fdedd',
        'hotelamour',
        'b7145842-962f-11e3-8b8e-abca0f9fdedd',
        'cafeamour',
        '19fe2713-9630-11e3-8b8e-abca0f9fdedd' ]
];

var nodes = {
    id: '',
    label: ''
};


var edges = {
    source: '',
    target: '',
    id: ''
};

for (var i = 0; i < myObj.length; i++) {
    nodes['id'].push(myObj[i][0]);
    nodes['label'].push(myObj[i][1]);
    nodes['id'].push(myObj[i][2]);
    nodes['label'].push(myObj[i][3]);
    edges['source'].push(myObj[i][0]);
    edges['target'].push(myObj[i][2]);
    edges['id'].push(myObj[i][4]);
}

Already searched on StackOverflow and elsewhere, but none of the solutions provided worked for me, probably because it's a multi-dimensional array that I need and of a slightly different structure than the object (see the code above).

Thank you for your help!

Was it helpful?

Solution

This code works:

   var nodes_object = myObj;
   var g = {
        nodes: [],
        edges: []
    };

    for (var i = 0; i < nodes_object.length; i++) {

        g.nodes.push({
            id: nodes_object[i][0],
            label: nodes_object[i][1]
        });

        g.nodes.push({
            id: nodes_object[i][2],
            label: nodes_object[i][3]
        });

        g.edges.push({
            source: nodes_object[i][0],
            target: nodes_object[i][2],
            id: nodes_object[i][4],
            edge_context: nodes_object[i][5]
        });

    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top