Write objects into file with Node.js

后端 未结 6 1716
滥情空心
滥情空心 2020-12-02 10:53

I\'ve searched all over stackoverflow / google for this, but can\'t seem to figure it out.

I\'m scraping social media links of a given URL page, and the function re

6条回答
  •  生来不讨喜
    2020-12-02 11:34

    Building on what deb2fast said I would also pass in a couple of extra parameters to JSON.stringify() to get it to pretty format:

    fs.writeFileSync('./data.json', JSON.stringify(obj, null, 2) , 'utf-8');
    

    The second param is an optional replacer function which you don't need in this case so null works.

    The third param is the number of spaces to use for indentation. 2 and 4 seem to be popular choices.

提交回复
热议问题