0

How do you source the data of multiple objects from a text file in JavaScript? I'm creating a program that will work with employee data and each employee will be treated as an object; I want the object data to come from an external source (i.e. a text file or an Excel spreadsheet), instead of having to explicitly write out the data for each employee within the code of the program. The multiple employee objects will be pushed into an array and then I'll interact with them in various ways. In the code below, I've had to explicitly write out employee data (see below). I want to source that data from some external source instead. Thanks!

function Employee (name, roomID, email)
{
    this.name = name;
    this.roomID = roomID;
    this.email = email; 
}

function EList()
{
    this.employees = [];
    this.firstindex = 0;
}

var employeeList = new EList();

var employee1 = new Employee("Aman Mundra", "D3-1", "aman.s.mundra@gmail.com"); //parameters inside parenthesis 
var employee2 = new Employee("John Doe", "D4-1", "john.doe@gmail.com");
var employee3 = new Employee("Jane Doe", "D4-2", "jane.doe@gmail.com");
employeeList.employees.push(employee1, employee2, employee3)
Aman
  • 5
  • 1
  • 3
  • 3
    use ajax to grab the file from your server, digest as needed. – dandavis Jun 30 '15 at 21:01
  • What sources, specifically, are you asking for? – nobrandheroes Jun 30 '15 at 21:01
  • So, typically one would use a database for that type of information, rather than a text file or Excel spreadsheet, because you won't be able to edit the text file or spreadsheet while the web server is reading it (depending on the web server, caching, etc.). – Heretic Monkey Jun 30 '15 at 21:29
  • Too broad, we can only help if you show what you've tried. Stack Overflow is not for suggestions, it's for making code work. – Ruan Mendes Jun 30 '15 at 21:30

2 Answers2

0

You might be looking for something along these lines if you can work with a CSV file: Javascript loading CSV file into an array

Why JavaScript? What are the other moving parts?

Community
  • 1
  • 1
Bennett Elder
  • 118
  • 1
  • 8
  • What I need to do is take employee data from a database (particularly each employee's assigned Room #) an then use a programming language (I chose JavaScript) to search through a PDF map of an office and zoom to that particular office. Is there a way to do this in JavaScript? I ended up choosing JavaScript because this project will eventually be posted online for employees, and also because the Adobe API is a library for JavaScript; do you have any alternate suggestions? Thanks! – Aman Jul 01 '15 at 13:26
0

What you are talking about is JSON. Make sure you are using JSON and the functions are already written for you. See json.com and json.org.

Particularly you would use JSON.parse to parse your text file, which would come in the form of a string after you have ajaxed your file in.

Here is the documentation for JSON.parse.