数组拍平/扁平化

营业啦~ 主要是记录一下 方便自己复习

题目:请写出一个数组拍平函数。效果如下: var arr=['a', ['b', 'c'], 2, ['d', 'e', 'f'], 'g', 3, 4];       flat(arr)    //a,b,c,2,d,e,f,g,3,4

解决:

方法一:使用toString方法先将arr转换为一个字符串, 再以split分割为数组,再将数组里面的元素转换为数字类型

1 var arr =['a', ['b', 'c'], 2, ['d', 'e', 'f'], 'g', 3, 4];
2 
3             function flat(arr) {
4                 return arr.toString().split(',').map(function(item){
5                     return Number(item)
6                 })
7             }
8             console.log(flat(arr))

方法二: toString 格式转换 与方法一类似 都是隐士类型转换

 1     var arr = ['a', ['b', 'c'], 2, ['d', 'e', 'f'], 'g', 3, 4];
 2       // 方法二:toString(格式转换)
 3 
 4       var flag = function (arr) {
 5           let toString = Array.prototype.toString;
 6           Array.prototype.toString = function () {
 7               return this.join(',');
 8           };
 9           let result = arr + '';
10           Array.prototype.toString = toString;
11           return result;
12       };
13 
14       console.log(flag(arr));

方法三: valueOf(格式转换) 与方法一 二类似 都是隐士类型转化原理

 1     // 方法三:valueOf(格式转换)
 2       Array.prototype.valueOf = function () {
 3           return this.join(',');
 4       };
 5 
 6       var flat = function (arr) {
 7           return arr + '';
 8       };
 9 
10       console.log(flat(['a', ['b', 'c'], 2, ['d', 'e', 'f'], 'g', 3, 4]));

方法四: 利用reduce特性

1     function flat (arr) {
2         return newArr = arr.reduce((a,b) => {
3           return a.concat(b)
4         },[])
5       }
6     var arr = ['a', ['b', 'c'], '2', ['d', 'e', 'f'], 'g', 3, 4];
7      console.log(flat(arr));

方法五:利用递归

 1       function flat (array) {
 2           var result = [];
 3           var each = function (arr) {
 4               arr.forEach(item => {
 5                   if (item instanceof Array) {
 6                       each(item);
 7                   } else {
 8                       result.push(item);
 9                   }
10               });
11           };
12           each(array);
13           return result.join(',');
14       }
15       var arr = ['a', ['b', 'c', [7, 8]], 2, ['d', 'e', 'f'], 'g', 3, 4];
16       console.log(flat(arr));

方法六: ES6的遍历器 Iterator 给数据结构增加遍历器必须增加一个next方法

 1     // Iterator
 2       Array.prototype[Symbol.iterator] = function () {
 3           let arr = [].concat(this);
 4           // arr=['a', ['b', 'c'], '2', ['d', 'e', 'f'], 'g', 3, 4]
 5           let getFirst = function (array) {
 6               let first = array.shift();
 7               if(first instanceof Array){
 8                 if(first.length>1){
 9                   arr=first.slice(1).concat(array);
10                 }
11                 first=first[0];
12               }
13               return first;
14           };
15           return {
16               next: function () { //类似与遍历
17                   let item = getFirst(arr);
18                   if (item) {
19                       return {
20                           value: item,
21                           done: false,
22                       };
23                   } else {
24                       return {
25                           done: true,
26                       };
27                   }
28               },
29           };
30       };
31       var flat = function (arr) {
32           let r = [];
33           for (let i of arr) { r.push(i); } // i 已经是单个元素
34           return r.join(',');
35       };
36       var arr = ['a', ['b', 'c'], '2', ['d', 'e', 'f'], 'g', 3, 4];
37       console.log(flat(arr));
原文地址:https://www.cnblogs.com/chengxuyuana/p/10517879.html