Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
Is there a way to populate a data structure from a text file in JS?

I am trying to use vis.js to draw out network topologies. The required format for entering nodes and edges is as follows:

var nodes = new vis.DataSet([
        {id: 1, label: 'Node 1'},
        {id: 2, label: 'Node 2'},
        {id: 3, label: 'Node 3'},
        {id: 4, label: 'Node 4'},
        {id: 5, label: 'Node 5'}
    ]);
    var edges = new vis.DataSet([
        {from: 1, to: 3},
        {from: 1, to: 2},
        {from: 2, to: 4},
        {from: 2, to: 5}
    ]);

I want to know how I can enter this data directly from a text file. I have two separate .txt files for the nodes and edges. I need to read them and populate the nodes and edges variables in my js script.

The nodes .txt file looks like this

{id: 1, label: 'Node 1'},
{id: 2, label: 'Node 2'},
{id: 3, label: 'Node 3'},
{id: 4, label: 'Node 4'},
{id: 5, label: 'Node 5'}

And the edges .txt file looks like this

{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}

Please share the code and thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would recommend to use a JSON file for this. Not only will you get syntax highlighting and formatting in your IDE, but it can also be imported very easily using Node.js: require('./your-data-file.json') and will be parsed to a JS object out of the box.

{
  "nodes": [
    { "id": 1, "label": "Node 1"},
    { "id": 2, "label": "Node 2"},
    { "id": 3, "label": "Node 3"},
    { "id": 4, "label": "Node 4"},
    { "id": 5, "label": "Node 5"}
  ]
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda