Jason
Jason

Reputation: 1

how to convert js object to js literal?

I am using nodejs. I have an object.

const objA = { key : 'value' };

the target is to generate a new file obja.js, with the same literals inside the file, not JSON literal. How can I achieve this? If I use

let result = JSON.stringfy(objA);

result would be a JSON literal:

{ "key" : "value" }

But I want a js literals:

{ key : 'value' }

Upvotes: 0

Views: 63

Answers (1)

Abhineet
Abhineet

Reputation: 60

You can use util here, its also handy when you want to console.log any object

const objA = { keyword : 'value' };
const fs = require('fs');
const util = require('util')
const objA1 = util.inspect(objA, {showHidden: false, depth: null})
fs.writeFileSync('./obja.js', objA1 );

Upvotes: 1

Related Questions