Create JavaScript Array Of Objects From CSV Value

node v18.11.0
version: 1.0.0
endpointsharetweet
const csv = ` WPS WPM WPT WPG WTS WTM WTT WTG TDS TDM TDT TDG MAS MAM MAT MAG ELS ELM ELT ELG ECS ECM ECT ECG MSS MSM MST MSG CSS CSM CST CSG FName Lname Total 183 179 362 B 37 31 68 B 36 28 64 B 41 29 70 B 40 34 74 B 39 27 66 B 33 15 48 C 32 33 65 B Tanveer Singh 817 172 150 322 B 31 26 57 C+ 18 9 27 E 28 9 40 O 31 21 52 C+ 19 13 32 E 34 4 40 D 22 17 40 D Deepanshu Nalagaria NA 168 172 340 B 41 31 72 B+ 22 32 54 C+ 38 32 70 B• 34 35 69 B 31 29 60 B 34 31 65 B 28 19 47 C Aspreet Singh 777 170 178 346 B 40 34 74 B+ 38 30 68 B 46 41 87 A 39 38 77 B+ 45 40 65 A 42 40 82 A 41 41 82 A Gurkirat Singh 903 182 201 383 B+ 27 29 56 C+ 20 20 40 D 28 28 56 C 23 22 45 C 28 23 51 C+ 34 19 53 C+ 23 29 52 C+ Gurjot Singh 736 156 170 326 B 32 33 65 B 37 31 68 B 32 0 32 E 29 32 61 B 33 33 66 B 35 15 50 C+ 30 29 59 C+ Arsh Lakhani NA 178 176 354 B+ 39 45 64 A 31 29 60 B 48 46 94 A 41 34 75 B+ 44 48 92 A+ 41 42 83 A 32 35 67 B Bittu Kumar 909 185 173 358 B+ 40 33 73 B+ 10 20 30 E 39 26 65 B 38 32 70 B+ 35 32 67 B 34 33 67 B 32 32 64 B Ramanjeet Singh NA 184 171 355 B+ 41 25 66 B 25 14 40 D 27 15 42 0 32 27 59 C+ 23 25 48 C 33 22 55 C+ 29 30 59 C Aryash Lakkhiwal 724 196 195 391 B+ 44 37 81 A 43 37 60 A 29 20 49 C 30 33 63 B 36 33 69 B 37 21 58 C+ 33 40 73 B+ Tarun Kotyan 864 192 174 366 B+ 34 35 69 B 18 34 52 C+ 43 32 75 B+ 35 29 64 B 27 28 55 C+ 33 18 51 C+ 32 37 69 B Tanish Dhiman 801 170 190 360 B+ 40 30 70 B+ 33 32 65 B 39 31 70 B+ 38 31 69 B 33 22 55 C+ 35 19 54 C+ 30 28 58 C+ Harshit Jagga 801 ` function CsvToArr(stringVal, splitter) { const [keys, ...rest] = stringVal.trim().split('\n').map((item) => item.split(splitter)) const formedArr = rest.map((item) => { const object = {} keys.forEach((key, index) => object[key] = item.at(index)) return object }) return formedArr; } CsvToArr(csv, ' ')
Loading…

no comments

    sign in to comment