Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda