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", "[email protected]"); //parameters inside parenthesis 
var employee2 = new Employee("John Doe", "D4-1", "[email protected]");
var employee3 = new Employee("Jane Doe", "D4-2", "[email protected]");
employeeList.employees.push(employee1, employee2, employee3)
4
  • 3
    use ajax to grab the file from your server, digest as needed. Commented Jun 30, 2015 at 21:01
  • What sources, specifically, are you asking for? Commented Jun 30, 2015 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.). Commented Jun 30, 2015 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. Commented Jun 30, 2015 at 21:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.