1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| var a = [0,1]; var b = [2,3]; console.log(a.concat(b));
function flatten(arr){ var res = [], i = 0, length = arr.length; for(;i < length; i++){ res = res.concat(arr[i]) } return res; }
function flatten(arr, result) { if (!result) { result = [] } for (var i = 0; i < arr.length; i++) { if(arr[i].constructor == Array) { flatten(arr[i], result) } else { result.push(arr[i]) } } return result } flatten([[1, 2], [3, 4, 5], [6, 7, 8, 9], [11,12,[12,13,[14]]], 10, 11])
function flatten(arr) { arr = [].concat.apply([], arr) return arr.some(Array.isArray) ? flatten(arr) : arr; }
var arr = [1,2,4,[4,5],[4,[23,34],5]] console.log(flatten(arr));
function flatten(arr) { return eval(`[${arr + ""}]`); } var arr = [1,2,4,[4,5],[4,[23,34],5]] console.log(flatten(arr));
|