-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-c.js
43 lines (34 loc) · 1005 Bytes
/
index-c.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import * as fs from 'fs'
const lista = fs.readFileSync('./lista.csv', 'utf-8')
// console.log(lista)
// console.log(typeof lista)
const newList = lista.toString().split('\r\n')
console.log(newList)
let result = []
let headers = newList[0].split(',')
console.log(headers)
for (let i = 1; i < newList.length - 1; i++) {
let obj = {}
let str = newList[i]
// console.log(str)
let s = ''
let flag = 0
for (let ch of str) {
if (ch === '"' && flag === 0) {
flag = 1
}
else if (ch === '"' && flag == 1) flag = 0
if (ch === ', ' && flag === 0) ch = '|'
if (ch !== '"') s += ch
}
let properties = s.split("|")
for (let j in headers) {
if (properties[j].includes(", ")) {
obj[headers[j]] = properties[j].split(", ").map(item => item.trim())
}
else obj[headers[j]] = properties[j]
}
result.push(obj)
}
let json = JSON.stringify(result);
fs.writeFileSync('content.json', json);