let testcsv name role country Sarene Help Desk Operator Thailand Olvan Nurse Practicioner China Janos Cost Accountant China Dolph Assistant Manager China Ariela Database Administrator I Azerbaijan Lane Environmental Tech Indonesia Griselda Senior Quality Engineer Portugal Manda Physical Therapy Assistant Brazil Leslie Information Systems Manager Japan Aleen Cost Accountant Canada function csvToArray str delimiter str str trim slice from start of text to the first n index use split to create an array from string by delimiter const headers str slice 0 str indexOf n split delimiter console log headers headers slice from n index 1 to the end of the text use split to create an array of each csv value row const rows str slice str indexOf n 1 split n Map the rows split values from each row into an array use headers reduce to create an object object properties derived from headers values the object passed as an element of the array const arr rows map function row const values row split delimiter const el headers reduce function object header index object header values index return object return el return the array return arr let data csvToArray testcsv let json JSON stringify data console log data console log ready
text use split to create an array of each csv value row const rows str slice str indexOf n 1 split n Map the rows split values from each row into an array use headers reduce to create an object object properties derived from headers values the object passed as an element of the array const arr rows map function row const values row split delimiter const el headers reduce function object header index object header values index return object return el return the array return arr let data csvToArray testcsv let json JSON stringify data console log data console log ready