const fs = require("fs")
function objectToString(obj) {
const objJson = JSON.stringify(obj, null, "\t")
const strArr = objJson.split(/\r\n|\n|\r/gm).map(item => {
return item.replace(/"/, "").replace(/\"\:/, ":")
})
const objStr = strArr.join("\r\n")
return objStr
}
let obj = { a: 1, b: 2, c: 3 }
let str = objectToString(obj)
let content = `
let a = ${str}
`
fs.writeFile("test.js", content, err => {
if (err) {
console.log("writeFile fail")
}
console.log("writeFile success")
})
写入文件中之后在test.js文件中数据是