var arr = [[1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11, [12, 13, [14, 15, 16]]], 17],
str = arr.toString(), // '1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17'
strArr = str.split(','), // ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17']
res = strArr.map(item => parseInt(item)) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
var arr = [[1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11, [12, 13, [14, 15, 16]]], 17],
str = arr.toString(), // '1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17'
jsonStr = '[' + str + ']', // '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]'
res = JSON.parse(jsonStr) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
function flat(array) {
/* 递归 */
var _res = [],
dfs = function (arr) {for (let i = 0; i < arr.length; i++) {Object.prototype.toString.call(arr[i]) === '[object Array]'
? dfs(arr[i])
: _res.push(arr[i])
}
}
dfs(array)
return _res
}
function flat(array) {
/* 迭代 */
var queue = [array],
res = [],
next
while (queue.length) {next = queue.shift()
Object.prototype.toString.call(next) === '[object Array]'
? Array.prototype.push.apply(queue, next)
: res.push(next)
}
return res
}