node.js - how to write an array to file

前端 未结 5 1516
礼貌的吻别
礼貌的吻别 2020-12-13 03:46

I have a sample array as follows

var arr = [ [ 1373628934214, 3 ],
  [ 1373628934218, 3 ],
  [ 1373628934220, 1 ],
  [ 1373628934230, 1 ],
  [ 1373628934234,         


        
5条回答
  •  天涯浪人
    2020-12-13 04:17

    Remember you can access good old ECMAScript APIs, in this case, JSON.stringify().

    For simple arrays like the one in your example:

    require('fs').writeFile(
    
        './my.json',
    
        JSON.stringify(myArray),
    
        function (err) {
            if (err) {
                console.error('Crap happens');
            }
        }
    );
    

提交回复
热议问题