Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!