Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
1  
Who put the minus?! Why? –  deemeetree Feb 15 at 17:19

1 Answer 1

up vote 2 down vote accepted

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]
        });

    };
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.