Q: What is array flattening?

A: Turning A multidimensional array into A one-dimensional array is array flattening

Such as:

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

  • Stretch to a one-dimensional array [1, 2, 3, 4, 5, 6, 7, 8, 9]

  • Stretch to a two-dimensional array [1, 2, 3, 4, 5, 6, 7, 8, [9]]

  • Stretch to a three-dimensional array [1, 2, 3, 4, 5, 6, 7, 8, [7, 8, [9]]

  • Stretch a layer [1, 2, 3, 4, [5, 6, [7, 8, [9]]]]

  • Stretch two layers [1, 2, 3, 4, 5, 6, 7, 8, [7, 8, [9]]

 

Q: What does array flattening do?

A: ACTUALLY, I don’t know that either. I just stay at the theoretical level and don’t have any practical experience. But according to my teacher, it can handle complex background data. If you think about it, if you receive a JSON file with layers and layers of data nested like an onion, you’re going to have trouble accessing the data, so you need to flatten it out.

 

There you go.


@[toc]


1. ES6 flat ()

Var newArray = arr.flat(depth)

  • Depth: Specifies the number of layers to be stretched. The default value is 1.

 

By default, no arguments are written, just stretch them one layer.

The argument is Infinity and stretched directly into a one-dimensional array

2. Use the JSON method


let arr = [1.2[3.4[5.6[7.8[9]]]]]

 

let res = JSON.stringify(arr) / / res = [1, 2, 3, 4, [5, 6, [7, 8, [9]]]]] (string)

res = res.replace(/\[|\]/g.' '/ / res = 1,2,3,4,5,6,7,8,9 (string)

res = '[' + res + '] '               / / res =,2,3,4,5,6,7,8,9 [1] (string)

res = JSON.parse(res)                 //res = [1, 2, 3, 4, 5, 6, 7, 8, 9](array)

Copy the code

If you don’t mind, you can write one sentence


let res = JSON.parse(` [The ${JSON.stringify(arr).replace(/(\[|\])/g.' ')}] `)

Copy the code

3. The recursion


function flat(arr) {

       let result = []

       for (let i of arr) {

              i instanceof Array ? result = result.concat(flat(i)) : result.push(i)

              // I is an instance of Array. If it is, keep recursively expanding, if it is not, put I in the result array

       }

       return result

}

flat(arr)

Copy the code

4. The reduce () recursion

Reduce () method introduction:

  • Syntax: array.reduce(function(Total, currentValue, currentIndex, ARR), initialValue)

  • Parameters:

       – function(total,currentValue, index,arr)

– total Required. The initial value, or the return value at the end of the calculation.

– currentValue Mandatory. The current element

– currentIndex Optional. The index of the current element

– ARr Optional. The array object to which the current element belongs.

– initialValue This parameter is optional. The initial value passed to the function


function flat(arr) {

       return arr.reduce((a, i) = > {

              return a.concat(i instanceof Array ? flat(i) : i)

       }, [])

}

flat(arr)

Copy the code
  • function –(a, i) => {... }

       – total    – a

       – currentValue      – i

  • initialValue -[]

 

5. The iteration

Some () ¶

The some() method is used to check whether elements in an array satisfy the specified condition (provided by the function).

The some() method executes each element of the array in turn:

  • If one element meets the criteria, the expression returns true, and the remaining elements are not tested.

  • If no element satisfies the condition, false is returned.

Note: Some () does not detect an empty array and does not alter the original array.

  • Grammar: array. Some (function (currentValue, index, arr), thisValue)

  • Parameters:

– function(currentValue, index,arr) is mandatory. Function, which is executed by each element in the array

– currentValue This parameter is mandatory. The value of the current element

– index Optional. The index value of the current element

– ARr Optional. The array object to which the current element belongs

– thisValue This parameter is optional. Object is used when the callback is executed, passed to the function, and used as the value of “this”.

If thisValue is omitted, the value of “this” is” undefined”


while (arr.some(Array.isArray)) { arr = [].concat(... arr); }Copy the code

Review.