var arr=[ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];git
1 .github
Array.from(new Set(arr.flat(Infinity))).sort((a,b)=>{ return a-b})
2 .数组
function flatten(arr) { while (arr.some(item => Array.isArray(item))) { arr = [].concat(...arr); } return arr; } Array.from(new Set(flatten(arr))).sort((a, b) => { return a - b })
3.spa
Array.from(new Set(arr.toString().split(',').map((v)=>{return parseInt(v,10)}))).sort((a,b)=>{ return a-b})
[...new Set('ababbc')].join('') // "abc" 去除字符串里面的重复字符。
const set = new Set([1, 2, 3, 4, 4]); [...set] // [1, 2, 3, 4]
console.log(...[1, 2, 3]) //1 2 3 console.log([1, 2, 3]) // [1, 2, 3]
该运算符将一个数组,变为参数序列。code
只能展平一层blog
[1,2,3].concat([4,5,6],[7,8,9]) // [1, 2, 3, 4, 5, 6, 7, 8, 9] ['a','b','c'].concat(1,[2,3],[[4,5]]) // ["a", "b", "c", 1, 2, 3, [4,5]]
[1, 2, [3, 4]].flat() // [1, 2, 3, 4]
[1, 2, [3, [4, 5]]].flat() // [1, 2, 3, [4, 5]] 默认为1。 [1, 2, [3, [4, 5]]].flat(2) // [1, 2, 3, 4, 5]
[1, [2, [3]]].flat(Infinity) // [1, 2, 3] // 若是无论有多少层嵌套,都要转成一维数组,能够用`Infinity`关键字做为参数。
[1, 2, , 4, 5].flat() // [1, 2, 4, 5] 若是原数组有空位,`flat()`方法会跳过空位。
toString()展平后每一个数组中的至是字符串,可根据须要再转换ip
var arr=[ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]; arr.toString();
参考文章字符串