diff --git a/json.html b/json.html index 2d7ce52..9a46eec 100644 --- a/json.html +++ b/json.html @@ -10,5 +10,6 @@

スプレッドシート取得

+
 
\ No newline at end of file diff --git a/sample.js b/sample.js index 8cce5a1..511faeb 100644 --- a/sample.js +++ b/sample.js @@ -7,11 +7,29 @@ /*成功した処理*/ .then(data => { //JSONから配列に変換 - var array = data; - for (var i = 0; i < array.length; i++) { +/* + var quizArray = data; + + for (var i = 0; i < quizArray.length; i++) { var quizList = document.createElement('li'); - quizList.textContent = array[i]; + quizList.textContent = quizArray[i]; document.getElementById('list').appendChild(quizList); } - console.table(array); -}); \ No newline at end of file + + console.table(quizArray); +*/ + console.log(JSON.stringify(data, null, 2)); + console.log(json2csv(data)); +}); + +function json2csv(json) { + var header = Object.keys(json[0]).join(',') + "\n"; + + var body = json.map(function(d){ + return Object.keys(d).map(function(key) { + return d[key]; + }).join(','); + }).join("\n"); + + return header + body; +} \ No newline at end of file