Converting an object to a string

后端 未结 30 2501
北荒
北荒 2020-11-22 03:29

How can I convert a JavaScript object into a string?

Example:

var o = {a:1, b:2}
console.log(o)
console.log(\'Item: \' + o)

Output:

30条回答
  •  天命终不由人
    2020-11-22 04:16

    stringify-object is a good npm library made by the yeoman team: https://www.npmjs.com/package/stringify-object

    npm install stringify-object
    

    then:

    const stringifyObject = require('stringify-object');
    stringifyObject(myCircularObject);
    

    Obviously it's interesting only if you have circular object that would fail with JSON.stringify();

提交回复
热议问题