1、数组扁平化
数组扁平化是指将一个多维数组变为一个一维数组
1  | const arr = [1, [2, [3, [4, 5]]], 6];  | 
方法一:flat() 方法
    let newArray = arr.flat(depth)
flat()方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。 其中,depth指定要提取嵌套数组的结构深度,默认值为1。但使用 Infinity 作为深度,展开任意深度的嵌套数组
1  | const res1 = arr.flat(Infinity);  | 
方法二:使用正则
1  | const res2 = JSON.stringify(arr).replace(/\[|\]/g, '').split(',');  | 
方法三:正则改良版本
1  | const res3 = JSON.parse('[' + JSON.stringify(arr).replace(/\[|\]/g, '') + ']');  | 
方法四:使用reduce
1  | const flatten = arr => {  | 
方法五:函数递归
1  | const res5 = [];  | 
2、数组去重
1  | const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}];  | 
方法一:Set去重
1  | const res1 = Array.from(new Set(arr));  | 
方法二:两层for循环+splice
1  | const unique1 = arr => {  | 
方法三:indexOf
1  | const unique2 = arr => {  | 
方法四:include
1  | const unique3 = arr => {  | 
方法五:filter
1  | const unique4 = arr => {  | 
3、类数组转化为数组
类数组是具有length属性,但不具有数组原型上的方法。常见的类数组有arguments、DOM操作方法返回的结果.
方法一:Array.from
1  | Array.from(document.querySelectorAll('div'));  | 
方法二:Array.prototype.slice.call()
1  | //call的应用-借用其他类型中的方法  | 
方法三:扩展运算符
1  | [...document.querySelectorAll('div')]  | 
方法四:利用concat
1  | //apply的应用--把数组展开传递给之前的方法  | 
4、Array.prototype.filter

1  | Array.prototype.filter = function(callback, thisArg) {  |