JavaScriptで多次元配列を1次元配列に変換する方法
方法1
flatten( )を使う
- install
.shell
npm install flatten
- example
.js
> var flatten = require('flatten');
> var array = [['a','b','c'],['d']];
> console.log(flatten(array));
[ 'a', 'b', 'c', 'd' ]
方法2
Array.prototype.concat.apply( )を使う
- example
.js
> var array = [['a','b','c'],['d']];
> console.log(Array.prototype.concat.apply([],array));
[ 'a', 'b', 'c', 'd' ]