0

EDIT: How do I go from this :

    tickets = [ 
{ date: "Jan 05,2016", time: "08:19 AM", name: "Bernie Sanders" },
{ date: "Jan 05,2016", time: "09:29 AM", name: "Donald Trump" },
{ date: "Jan 05,2016", time: "09:31 AM", name: "Donald Trump" },
{ date: "Jan 05,2016", time: "09:34 AM", name: "Donald Trump" },
{ date: "Jan 05,2016", time: "09:34 AM", name: "Bernie Sanders" },
{ date: "Jan 05,2016", time: "09:07 AM", name: "Mike Huckabee"}
];

    ...

to this:

tickets = [ 
{ date: "Jan 05,2016", time: "08:19 AM", name: "Bernie Sanders", total: 1 },
{ date: "Jan 05,2016", time: "09:29 AM", name: "Donald Trump", total: 1 },
{ date: "Jan 05,2016", time: "09:31 AM", name: "Donald Trump", total: 2 },
{ date: "Jan 05,2016", time: "09:34 AM", name: "Donald Trump", total: 3 },
{ date: "Jan 05,2016", time: "09:34 AM", name: "Bernie Sanders", total: 2 },
{ date: "Jan 05,2016", time: "09:07 AM", name: "Mike Huckabee", total: 1}
]

I looked through the popular libraries that deal with objects and arrays. For example, underscore.js, lodash.js, lazy.js. I was not able to find anything that will do this

Bum Son
  • 89
  • 1
  • 7
  • 2
    that's not csv format – Jaromanda X Jan 12 '16 at 01:26
  • I copied it from a csv file – Bum Son Jan 12 '16 at 01:27
  • 2
    show the RAW csv data - because that is *not likely* to be csv ... only 1 comma, and it's in the middle of the Date – Jaromanda X Jan 12 '16 at 01:28
  • 2
    that's not a csv file either ... do you know what the **C** in csv stands for? – Jaromanda X Jan 12 '16 at 01:43
  • @Burn Son, its look like tab separated spread sheet – Venkat.R Jan 12 '16 at 01:46
  • **CSV** would be like `"text here","text here" ... text will (generally) be quoted and each column (field) is separated by a **Comma**. What you are showing _might_ be **Tab** separated values (TSV) but it _certainly_ is _not_ CSV. – Stephen P Jan 12 '16 at 01:47
  • I opened the csv file, high litre it and selected copy. Then I pasted it in here – Bum Son Jan 12 '16 at 01:57
  • CSV format can have different separators, even though technically it may be TSV, it may still be called CSV... but yes, the separator is what you need to figure out to be able to split the file properly. – MrE Jan 12 '16 at 02:07
  • THats not the important part. The important part is how I can get the running total key value pair. Each time that person submits a ticket, the total increases by one. Counting unique instances of when someone sun nuts a ticket. I just need the logic I don't need the raw code – Bum Son Jan 12 '16 at 02:20

3 Answers3

2

assuming you don't need to support legacy browsers, vanillaJS has everything you need:

tickets = [ 
  { date: "Jan 05,2016", time: "08:19 AM", name: "Bernie Sanders" },
  { date: "Jan 05,2016", time: "09:29 AM", name: "Donald Trump" },
  { date: "Jan 05,2016", time: "09:31 AM", name: "Donald Trump" },
  { date: "Jan 05,2016", time: "09:34 AM", name: "Donald Trump" },
  { date: "Jan 05,2016", time: "09:34 AM", name: "Bernie Sanders" },
  { date: "Jan 05,2016", time: "09:07 AM", name: "Mike Huckabee"}
];

var x = document.getElementById('x');

var runningTotals = {};

var addTotalToTicket = function(ticket){
  if ( ! (ticket.name in runningTotals ) ) {
    runningTotals[ticket.name] = 0;  
  }
  runningTotals[ticket.name]++;
  ticket.votes = runningTotals[ticket.name];
  return ticket;
};

var ticketsWithTotals = tickets.map(addTotalToTicket);

x.innerHTML = JSON.stringify(ticketsWithTotals,null,"  ");
pre {
  background-color: #ded;
}
<pre><code id="x"></code></pre>
code_monk
  • 9,451
  • 2
  • 42
  • 41
1

Not mine but that's what you're looking for:

// Source: http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
// This will parse a delimited string into an array of
// arrays. The default delimiter is the comma, but this
// can be overriden in the second argument.

function CSVToArray(strData, strDelimiter) {
    // Check to see if the delimiter is defined. If not,
    // then default to comma.
    strDelimiter = (strDelimiter || ",");
    // Create a regular expression to parse the CSV values.
    var objPattern = new RegExp((
    // Delimiters.
    "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
    // Quoted fields.
    "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
    // Standard fields.
    "([^\"\\" + strDelimiter + "\\r\\n]*))"), "gi");
    // Create an array to hold our data. Give the array
    // a default empty first row.
    var arrData = [[]];
    // Create an array to hold our individual pattern
    // matching groups.
    var arrMatches = null;
    // Keep looping over the regular expression matches
    // until we can no longer find a match.
    while (arrMatches = objPattern.exec(strData)) {
        // Get the delimiter that was found.
        var strMatchedDelimiter = arrMatches[1];
        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        if (strMatchedDelimiter.length && (strMatchedDelimiter != strDelimiter)) {
            // Since we have reached a new row of data,
            // add an empty row to our data array.
            arrData.push([]);
        }
        // Now that we have our delimiter out of the way,
        // let's check to see which kind of value we
        // captured (quoted or unquoted).
        if (arrMatches[2]) {
            // We found a quoted value. When we capture
            // this value, unescape any double quotes.
            var strMatchedValue = arrMatches[2].replace(
            new RegExp("\"\"", "g"), "\"");
        } else {
            // We found a non-quoted value.
            var strMatchedValue = arrMatches[3];
        }
        // Now that we have our value string, let's add
        // it to the data array.
        arrData[arrData.length - 1].push(strMatchedValue);
    }
    // Return the parsed data.
    return (arrData);
}

function CSV2JSON(csv) {
    var array = CSVToArray(csv);
    var objArray = [];
    for (var i = 1; i < array.length; i++) {
        objArray[i - 1] = {};
        for (var k = 0; k < array[0].length && k < array[i].length; k++) {
            var key = array[0][k];
            objArray[i - 1][key] = array[i][k]
        }
    }

    var json = JSON.stringify(objArray);
    var str = json.replace(/},/g, "},\r\n");

    return str;
}

JSFiddle here:

http://jsfiddle.net/sturtevant/AZFvQ/

MrE
  • 19,584
  • 12
  • 87
  • 105
1

Assuming that you want to get total count of entries for some person, you can use underscore.js groupBy() and reduce() functions

Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55