简体   繁体   English

javascript:尝试将数组展平仅一个级别

[英]javascript: Trying to flatten array only one level

I am trying to write a function to flatten an array. 我正在尝试编写一个函数来展平数组。 I have part of the function working and I need help in the other half. 我有部分功能正常工作,另一半我需要帮助。

flatten: function(anyArray, singleLevel) {
  if (singleLevel == true) {
      flatArray = Array.prototype.concat.apply([], anyArray);
      return flatArray;
  }
  flatArray = Array.prototype.concat.apply([], anyArray);
  if (flatArray.length != anyArray.length) {
      flatArray = someObject.array.flatten(flatArray);
  }
  return flatArray;
}

if I type 如果我打字

.flatten([[[1],[1,2,3,[4,5],4],[2,3]]], true);

I want it to flatten only one level: 我希望它只展平一个级别:

[[1],[1,2,3,[4,5],4],[2,3]]

The concat array method expects one or more arrays as arguments, whose elements will be appended: concat数组方法需要一个或多个数组作为参数,其元素将被追加:

[1].concat([2, 3], [4]) // [1, 2, 3, 4]

So if you are using apply , that will flatten another level: 因此,如果您正在使用apply ,那将使另一个级别变平:

[].concat.apply([1], [[2], [3]]) // === [1].concat([2], [3])

So you can either use push instead of concat , or call (or just direct invocation) instead of apply to get only a single flattening level. 所以你可以使用push而不是concat ,或者call (或者只是直接调用)而不是仅仅apply获得单个展平级别。

Modern JavaScript allows us to handle this very easily using a variety of techniques 现代JavaScript允许我们使用各种技术轻松处理这一问题

Using Array.prototype.flat - 使用Array.prototype.flat -

 const arr = [ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ] const flatArr = arr.flat(1) // 1 is default depth console.log(JSON.stringify(arr)) console.log(JSON.stringify(flatArr)) // [[1],[2,3,[4,5,[6]]],[7,[8,9]]] // [1,2,3,[4,5,[6]],7,[8,9]] 

Using Array.prototype.flatMap - 使用Array.prototype.flatMap -

 const arr = [ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ] const flatArr = arr.flatMap(x => x) console.log(JSON.stringify(arr)) console.log(JSON.stringify(flatArr)) // [[1],[2,3,[4,5,[6]]],[7,[8,9]]] // [1,2,3,[4,5,[6]],7,[8,9]] 

Using a spread argument to Array.prototype.concat 使用spread参数到Array.prototype.concat

 const arr = [ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ] const flatArr = [].concat(...arr) console.log(JSON.stringify(arr)) console.log(JSON.stringify(flatArr)) // [[1],[2,3,[4,5,[6]]],[7,[8,9]]] // [1,2,3,[4,5,[6]],7,[8,9]] 


Older version of JavaScript (ECMAScript 5 and below) can use techniques like Function.prototype.apply - 旧版本的JavaScript(ECMAScript 5及以下版本)可以使用Function.prototype.apply技术 -

 var arr = [ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ] var flatArr = Array.prototype.concat.apply([], arr) console.log(JSON.stringify(arr)) console.log(JSON.stringify(flatArr)) // [[1],[2,3,[4,5,[6]]],[7,[8,9]]] // [1,2,3,[4,5,[6]],7,[8,9]] 

Using Array.prototype.reduce - 使用Array.prototype.reduce -

 var arr = [ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ] var flatArr = arr.reduce((r, a) => r.concat(a), []) console.log(JSON.stringify(arr)) console.log(JSON.stringify(flatArr)) // [[1],[2,3,[4,5,[6]]],[7,[8,9]]] // [1,2,3,[4,5,[6]],7,[8,9]] 

Using a primitive for loop - 使用原语for循环 -

 var arr = [ [ 1 ], [ 2, 3, [ 4, 5, [ 6 ] ] ], [ 7, [ 8, 9 ] ] ] var flatArr = [] for (var i = 0; i < arr.length; i = i + 1) flatArr = flatArr.concat(arr[i]) console.log(JSON.stringify(arr)) console.log(JSON.stringify(flatArr)) // [[1],[2,3,[4,5,[6]]],[7,[8,9]]] // [1,2,3,[4,5,[6]],7,[8,9]] 

if you use ES6/ES2015 you can use spread operator. 如果您使用ES6 / ES2015,您可以使用传播运营商。 Something like this 像这样的东西

 console.log(...[[[1],[1,2,3,[4,5],4],[2,3]]]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM