1. Array flattening (also known as array dimensionality reduction)
flat()
The method recursively traverses the array at a specified depth and merges all elements with the elements in the subarray they traversed into into a new array.
const test = ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]] // When flat does not pass parameters, the default flat layer is flat() // ["a", "b", "c", "d", ["e", ["f"]], "g"] // flat pass in an integer parameter, integer is the number of flat layers(2) // ["a", "b", "c", "d", "e", ["f"], "g"] // When the Infinity keyword is used as a parameter, no matter how many layers are nested, it will be converted into a one-dimensional array.(Infinity) // ["a", "b", "c", "d", "e", "f", "g"] // Integer passed in <=0 will return to the original array and will not be flattened(0) (-1) // ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]] // If there are empty spaces in the original array, the flat() method will skip the empty spaces.["a", "b", "c", "d",,].flat() // ["a", "b", "c", "d"]
Method 1: Use the reduce method
Flat all at once
function flattenDeep(arr) { return (arr) ? ( (acc, cur) => [...acc, ...flattenDeep(cur)] , []) : [arr] } // testvar test = ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]] flattenDeep(test) // ["a", "b", "c", "d", "e", "f", "g"]
Implement the flat function:
function flat(arr, depth = 1) { return depth > 0 ? ((acc, cur) => { if((cur)) { return [...acc, ...flat(cur, depth-1)] } return [...acc, cur] } , []) : arr } // testvar test = ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]] // When no parameters are passed, the default flattening layer isflat(test) // ["a", "b", "c", "d", ["e", ["f"]], "g"] // Pass in an integer parameter, integer is the number of flattened layersflat(test, 2) // ["a", "b", "c", "d", "e", ["f"], "g"] // When the Infinity keyword is used as a parameter, no matter how many layers are nested, it will be converted into a one-dimensional array.flat(test, Infinity) // ["a", "b", "c", "d", "e", "f", "g"] // Integer passed in <=0 will return to the original array and will not be flattenedflat(test, 0) flat(test, -10) // ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]]; // If there are empty spaces in the original array, the flat() method will skip the empty spaces.var arr = ["a", "b", "c", "d",,] flat(arr) // ["a", "b", "c", "d"]
Method 2: Stack
All dimensionality reductions at one time
function flattenDeep(arr) { const result = [] // Copying the array element to the stack, and directly assigning the value will change the original array const stack = [...arr] // If the stack is not empty, loop through while ( !== 0) { const val = () if ((val)) { // If the array is put into the stack again, and a layer is expanded (...val) } else { // If it is not an array, insert it into the result array by header insertion method (val) } } return result } // testvar test = ["a", ["b", "c"], ["d", ["e", ["f"]], "g"]] flattenDeep(animals) // ["a", "b", "c", "d", "e", "f", "g"]
2. Deduplication of array
Method 1: Set (ES6)
function unique(arr) { return (new Set(arr)) } // orvar unique = arr => [...new Set(arr)] // testvar arr = [1, 2, 2, 3] unique(arr); // [1, 2, 3]
Method 2: reduce
function unique (arr) { return ().reduce((acc, cur) => { if ( === 0 || acc[ - 1] !== cur) { (cur); } return acc }, [])} ; // testvar arr = [1, 2, 2, 3] unique(arr); // [1, 2, 3]
Method 3: filter
function unique(arr) { return ( (element, index, array) => { return (element) === index }) } // testvar arr = [1, 2, 2, 3] unique(arr); // [1, 2, 3]
This is the article about JavaScript array deduplication and flattening functions. For more related contents of JS array deduplication and flattening functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!