formdata对象上传了csv文件,读取文件内容拼接成json对象

javascript读取csv js读取csv转json_json

var form = new FormData();
var files = $("#getfile")[0].files;
var reader = new FileReader();
    reader.readAsText( files[0],"gbk" );            //以文本格式读取
    reader.onload = function(evt){
        var data = evt.target.result;        //读到的数据
        console.log(data);
        splitdate = data.split(/\s+/) ;
        console.log(splitdate.length)
        var arr1=new Array();
        var arr2=new Array();
        var phone=new Array();
        var name=new Array();
        for(var i=0;i<splitdate.length;i++){
            var data =splitdate[i].split(",");
            arr1.push(data[0])
            arr2.push(data[2])
        }
        for(var i=1;i<arr1.length-1;i++){
            name.push(arr1[i])
        }
        for(var i=1;i<arr2.length-1;i++){
                        phone.push(arr2[i])
                    }
                    var userList = [];
                    for(var i=0;i<name.length;i++){
                        var json = {};
                        json.value = name[i];  
                        json.name = phone[i]; 
                        console.log(json);
                        userList.push(json);
                    }
                    console.log(userList)
                }

最终拼接成为key value格式的json对象