Skip to content

Latest commit

 

History

History
93 lines (64 loc) · 1.29 KB

数组扁平化的六种实现方式.md

File metadata and controls

93 lines (64 loc) · 1.29 KB

数组扁平化的六种实现方式

给定数组:

let arr = [1, [2, [3, [4, 5, [6, 7]]]], 8];
let str = JSON.stringify(arr);

实现其扁平化:

[1, 2, 3, 4, 5, 6, 7, 8]

1. flat()

var arr_flat = arr.flat(Infinity) // 参数:深度,默认为1

2. 正则+split()

var arr_flat = str.replace(/(\[|\])/g, '').split(',')

3. 正则+JSON.parse()

str = str.replace(/(\[|\])/g, '')
str = `[${str}]`
var arr_flat = JSON.parse(str)

4. for循环递归判断

function MyFlat (arr) {
  let result = [];
  let fn = function (arr) {
    for (var i = 0; i < arr.length; i++) {
      if (Array.isArray(arr[i])) {
        fn(arr[i])
      } else {
        result.push(arr[i])
      }
    }
  }
  fn(arr)
  return result;
}
var arr_flat = MyFlat(arr)

5. reduce

function reduceFlat (arr) {
  let fn = function (arr) {
    return arr.reduce((pre, cur) => {
      return pre.concat(Array.isArray(cur) ? fn(cur) : cur)
    }, [])
  }
  return fn(arr)
}
var arr_flat = reduceFlat(arr)

6. 扩展运算符+some()

function MyFlat (arr) {
  while(arr.some(Array.isArray)) {
    arr = [].concat(...arr)
  }
  return arr
}
var arr_flat = MyFlat(arr)