1·join和split
join将数组拼接为字符串 split将字符串转变为数组
结果的元素为字符串,可通过map遍历处理每一项
1 let temp = [1, [2, 3], [4, 5, [6, 7]]]; 2 let res = temp.join(',').split(',') 3 let res2 = res.map((val, idx) => { 4 return Number(val) 5 }) 6 console.log(res) 7 console.log(res2)
2· 递归
1 let temp = [1, [2, 3], [4, 5, [6, 7]]]; 2 let target = [] 3 let fn = (source) => { 4 for (let i of source) { 5 if (Array.isArray(i)) { 6 fn(i) 7 } else { 8 target.push(i) 9 } 10 } 11 } 12 fn(temp) 13 console.log('递归多维数组', target)
3·多维数组concat
1 let array = ['1', 2, [3, 4, [5, 6]], 7]; 2 let res2 = array.reduce((pre, cur) => { 3 return pre.concat(cur) 4 }, []) 5 console.log('多维数组concat', res2)
所以:concat只适合二维数组
1 let array = ['1', 2, [3, 4], 7]; 2 let res2 = array.reduce((pre, cur) => { 3 return pre.concat(cur) 4 }, []) 5 console.log('多维数组concat -- 二维', res2)
标签:console,数组,res2,let,一维,多维,concat From: https://www.cnblogs.com/xingnizhiren/p/17483480.html