Created
November 25, 2019 12:26
-
-
Save naosim/139bd4d077d75741ab1d96b0e905467d to your computer and use it in GitHub Desktop.
スプレッドシートの2次元配列をエンティティの配列に変換する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* スプレッドシートの2次元配列をエンティティの配列に変換する | |
* - 2次元配列は0番目はヘッダー(カラム名の配列)行、1番目以降にデータが並んでいること | |
* - ヘッダー行のカラム名は空でないこと | |
* - カラム名は値の最初の行だけ採用される 例: "身長\n[cm]" → カラム名は"身長"になる | |
* @param {[['name', 'age'], ['mike', 30]]} ary2d - スプレッドシートのような2次元配列 | |
* @return 例: [{name:"mike", age:30}, {name:"jiro", age:40}] | |
*/ | |
function toEntities(ary2d) { | |
var headers = ary2d[0] | |
.filter(function(v){ return v && v.trim().length > 0}) | |
.map(function(v) { return v.split('\n')[0].trim(); }); | |
return ary2d.slice(1).map(function(row) { | |
return headers.reduce(function(memo, key, i){ | |
memo[key] = row[i] | |
return memo; | |
}, {}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment