s950329
8/3/2016 - 10:46 AM

csv2json

csv2json

function csv2json(csv) {
    var allTextLines = csv.split(/\r\n|\n/);
    var keys = allTextLines[0].split(",");
    var data = [];

    for (var i = 1; i < allTextLines.length; i++) {
        var detail = allTextLines[i].split(",");
        var obj = {};
        for (var j = 0; j < keys.length; j++) {
            obj[keys[j]] = detail[j];
        }
        data.push(obj);
    }
    return data;
}