JavaScript Parsing Data into an Object
<p>interesting question for you. I'm trying to figure out the best way to parse information from my <code>.csv</code> file so that I have the raw data and can make comparisons. </p>

<p>What I'm trying to do right now is make an object for each person from the data that gets parsed in. So my csv file consists of:</p>

<pre><code>Position,Name,Salary,Gender,Category
Chef,John Doe,40000,M,0
Clerk,Jane Doe,20000,F,1
Server,Joe Doe,30000,M,2
</code></pre>

<p>That's a very minor portion, I have towards 1000 different people, where obviously positions and such repeat. In my code, I currently have the javascript:</p>

<pre><code>&lt;script type="text/javascript"charset="utf-8"&gt;
  var lines;
  var person;

  $(document).ready(function() {
    //fetch text file
    $.get('data.csv', function(data) {
      //split on new lines
      lines = data.split('\n');

      //This line didn't work and gave me an error? 
      //person = lines.split(',');

      person = data.split(',');

                //window.alert(lines[1]);
                //window.alert(person[5]);

    });

  });

&lt;/script&gt;
</code></pre>

<p>Anyways, I basically have tried a few things, didn't work and deleted it. So let me go over what I have tried. </p>

<ol>
<li>Each line works correctly, so line currently gives out the entire line by line. So I tried to then split each of those lines into player based off of <code>,</code>. This however gave me an error saying that <code>.split</code> is undefined.</li>
<li>So next thing I tried was just splitting from the data, and that works to some degree. It splits perfectly for the first line, then gets wonky when it comes to the new line. It skips the position <code>chef</code> and takes <code>John Doe</code>. Then when it gets to the <code>Category</code> it correctly takes <code>0</code> but also prints out <code>Chef</code> with 2 enters below it in the same window when using <code>window.alert(var[number])</code></li>
</ol>

<p>Then I realize, well this isn't really going to work anyways, I want to make each person it's own object. So I want every <code>person</code> to have it's own variables: <code>position, name, salary, gender, category</code></p>

<p>I looked that up and I get quite a few ways of how to make it, but none take the information in through a file but manually define it right then and there. So is there some way that someone can help me with this? Do I have to do some kind of for loop, and then something interesting like:</p>

<pre><code>var people = {
  position:"",
  name:"",
  salary:0,
  gender:"",
  category:,
};

for(i = 5; i != eof; i+5){
  people.position() = person[i];
  people.name() = person[i+1];
  people.salary() = person[i+2];
  people.gender() = person[i+3];
  people.category() = person[i+4];
}
</code></pre>

<p>Honestly not too sure, so any help with this would be great. Thanks!</p>

<hr>

<p>Edit:
With the assistance of Lucas, this is where I'm currently at:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
    &lt;head&gt;
        &lt;meta charset="utf-8"&gt;
    &lt;/head&gt;
    &lt;body&gt;

        &lt;script src="https://d3js.org/d3.v4.min.js"&gt;&lt;/script&gt;
        &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"&gt;&lt;/script&gt;

        &lt;script type="text/javascript"charset="utf-8"&gt;


          var people = [];

           $(document).ready(function() {
             $.get('data.csv', function(data){

               var lines = data.split('\n');
               var properties = ["position", "name", "salary", "points", "team"]

               for (var i = 0; i &lt; lines.length; i++){
                 var person = lines[i].split(',');
                 var tmpPlayer = {}
                 for (var n = 0; n &lt; person.length; n++){
                   tmpPlayer[properties[n]] = person[n]
                 }
                 people.push(tmpPlayer);
               }

             });

            //correctly displays the array of every person
            console.log(people);
            //gives the result undefined
            console.log(people[1]);
            //error Uncaught TypeError: Cannot read property 'name' of undefined
            console.log(people[1]["name"]);

           });

        &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>The <code>data.csv</code> file:</p>

<pre><code>Position,Name,Salary,Gender,Category
Chef,John Doe,40000,M,0
Clerk,Jane Doe,20000,F,1
Server,Joe Doe,30000,M,2
</code></pre>