Parsing CSV to Array of Hashes

I am trying to parse a CSV file and create an array of hashes. I implemented it using my own code but I don't think it is optimal. I am trying to use CSV-Parser but am only outputting an Array of Array's (not an Array of Hashes/Objects).

Here is an example of my first attempt without the library:

var fs = require('fs');
var readCSVData = function(data){
  var contactsArray = data.split("\n"); 
  var contactsArrayOfHashes = [];

for(var i=1; i < contactsArray.length; i++){
        var singleData = contactsArray[i].split(",");   

            contactsArrayOfHashes.push({
                first_name: singleData[0],
                last_name: singleData[1],
                months_since_contact: singleData[2],
                email_address: singleData[3]
            });
    }
    return contactsArrayOfHashes;
};

fs.readFile("contacts.csv","utf8", function(err, data){

    var csvData = readCSVData(data);
    console.log(csvData);       

});

Here is my attempt with the CSV Package (this is where I am only getting an Array of Arrays)

var fs = require('fs');
var csv = require("csv");

fs.readFile("contacts.csv","utf8", function(err, data){
    var friendsList = [];
    var csvData = readCSVData(data);
    csv.parse(data, function(err, parseData){
        for(var i=1; i < parseData.length; i++){
            friendsList.push(parseData[i]);
        }
    })

console.log(friendsList);

});

CSV File:

first_name,last_name,months_since_contact,email_address
John,Doe,0,john@john.com

How can I create an Array of Hashes using the CSV npm? Is there a way to map my array indexes with columns?

Ideally, I would like to map columns to rows

ex: my Array's appear to be Array's but they are mapped to my main fields in my CSV File like this:

var CSVData = [John,Doe,0,john@john.com]
CSVData[0]["email_address"] outputs --> john@john.com

I believe I've used this format before with ruby's CSV Class

From the docs:

Parser options

  • columns: List of fields as an array, a user defined callback accepting the first line and returning the column names or true if autodiscovered in the first CSV line, default to null, affect the result data set in the sense that records will be objects instead of arrays.