Created
May 14, 2018 12:48
-
-
Save tonis2/e5ddfad010487002590e64ab6efe3b6a to your computer and use it in GitHub Desktop.
CSV data to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const csvToJson = csv => { | |
| const response = []; | |
| const allLines = csv.split(/\r\n|\n/); | |
| const headers = allLines[0].split(/\t|,/).filter(value => value); | |
| for (let item = 1; item < allLines.length; item++) { | |
| const data = {}; | |
| const lineData = allLines[item].split(/\t|,/).filter(value => value); | |
| if (lineData.length == headers.length) | |
| lineData.forEach((line, index) => { | |
| data[headers[index]] = line; | |
| }); | |
| response.push(data); | |
| } | |
| return response; | |
| }; | |
| export default csvToJson; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment