2016-07-01 5 views
0

ich zwei Strings in einer js-Datei hinzufügen muss ich zu erzeugen bin mit writeFileSyncStrings hinzuzufügen, wenn ich Objekte in einem Array schieben

Dies ist der Code Beispiel

const svgs = []; 
var n = 2; 

for (var i = 0; i < n; i++) { 
    svgs.push(Cog.prototype.render()); 
    svgs.push(Manna.prototype.render()); 
} 

fs.writeFileSync(`client/data/${name}.json`, JSON.stringify(svgs)); 

Und das ist die JS-Datei ich erzeuge (zwei Objekte in einem Array)

[{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}},{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}}] 

Meine Frage ist, wie kann ich hinzufügen, zwei Strings, diesmal am Anfang const svgs = und export default svgs am Ende, um ähnliche

const svgs = [{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}},{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}}] export default svgs; 
+0

'fs.writeFileSync (' Client/data/$ {name} .json' "const SVGs =" + JSON.stringify mich auf meine Datei erzeugt zu machen (svgs)) + "export default svgs"; ' – Subburaj

+0

als Antwort hinzufügen, wird dies leicht korrigiert" const svgs = "+ JSON.stringify (svgs) .toString() +"; Export Standard svgs "+"; "); – Koala7

Antwort

1
fs.writeFileSync(client/data/${name}.json, "const svgs =" + JSON.stringify(svgs)) + "export default svgs" ; 
Verwandte Themen