Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript数组扁平化 #89

Open
GGXXMM opened this issue Jan 19, 2021 · 0 comments
Open

JavaScript数组扁平化 #89

GGXXMM opened this issue Jan 19, 2021 · 0 comments
Labels
⭐️ js js knowledge

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented Jan 19, 2021

数组扁平化

数组的扁平化,就是将多层嵌套的数组转换成只有一层的数组。

扁平化方法

1、递归

var arr = [1,2,[3,[4,5]]];
function flatten(arr) {
  let result = [];
  for(let item of arr) {
    if(Array.isArray(item)) {
      result = result.concat(flatten(item));
    }else{
      result.push(item);
    }
  }
  return result;
}
console.log(flatten(arr));// [1,2,3,4,5]

2、reduce

function flatten(arr) {
  return arr.reduce((prev, next)=>{
    return prev.concat(Array.isArray(next)? flatten(next) : next);
  }, [])
}
console.log(flatten(arr));// [1,2,3,4,5]

3、toString

function flatten(arr) {
  return arr.toString().split(',').map(item => +item);
}
console.log(flatten(arr));// [1,2,3,4,5]

4、es6扩展运算符...

function flatten(arr) {
  while(arr.some(item => Array.isArray(item))) {
    arr = [].concat(...arr);
  }
  return arr;
}
console.log(flatten(arr));// [1,2,3,4,5]

5、underscore.js flatten实现

/**
 * 数组扁平化
 * @param {Array} input 要处理的数组
 * @param {Boolean} shallow 是否只扁平一层
 * @param {Boolean} strict 是否严格处理元素,下面有解释
 * @param {*} output 这是为了方便递归而传递的参数
 * 源码地址:https://github.com/jashkenas/underscore/blob/745e9b7314064e66a7257f9b361030e6055980b8/underscore.js#L1015
 */
function flatten(input, shallow, strict, output){
  // 递归使用的时候会用到output
  output = output || [];
  var idx = output.length;

  for (var i = 0, len = input.length; i < len; i++) {

      var value = input[i];
      // 如果是数组,就进行处理
      if (Array.isArray(value)) {
          // 如果是只扁平一层,遍历该数组,依此填入 output
          if (shallow) {
              var j = 0, length = value.length;
              while (j < length) output[idx++] = value[j++];
          }
          // 如果是全部扁平就递归,传入已经处理的 output,递归中接着处理 output
          else {
              flatten(value, shallow, strict, output);
              idx = output.length;
          }
      }
      // 不是数组,根据 strict 的值判断是跳过不处理还是放入 output
      else if (!strict){
          output[idx++] = value;
      }
  }
  return output;
}

6. 直接调用es6 flat()

function flatten(arr) {
  return arr.flat(Infinity);
}

7. 正则和JSON方法处理

function flatten(arr) {
  var str = JSON.stringify(arr);
  str = str.replace(/(\[|\])/g, '');
  str = '['+str+']';
  return JSON.parse(str);
}
@GGXXMM GGXXMM added babel ⭐️ js js knowledge and removed babel labels Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐️ js js knowledge
Projects
None yet
Development

No branches or pull requests

1 participant