Converting JSON object to CSV format in JavaScript

前端 未结 9 822
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 04:37

I am trying to convert a JavaScript object set in to CSV format

You can get the idea about my Javascript object, if you put it in online JSON parser http://json.pars

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 05:16

    Similar to mightybruno's answer but this would allow separate access to the headers and content (by moving the join statements to later in the function) if desired.

    function objToCsv(data) {
        const headers = Object.keys(data[0]).join();
        const content = data.map(r => Object.values(r).join());
        return [headers].concat(content).join("\n");
    }
    

提交回复
热议问题