How to realize Array merging with js
This article mainly introduces js how to achieve array merging, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
1. Reduce () and reduceRight () iterate over all the items in the array and then build the final return value. Often used to accumulate array items, reduce the dimension of the array, remove the weight of the array, and count the number of occurrences.
2. The initial value of the merger is 0, and the cumulative result of each item is returned.
Example
Let arr = [[1, [2,4]], 3]; / / Array dimensionality reduction: reducefunction flattenDeep (arr) {return arr.reduce ((pre, cur) = > Array.isArray (cur)? Pre.concat (flattenDeep (cur)): pre.concat (cur), []);} let result = flattenDeep (arr); / / built-in dimensionality reduction. Flat receives a parameter, which represents dimensionality reduction several times, default reduction once, and input Infinity represents dimensionality reduction to 1-dimensional array (any infinite dimension can be reduced) arr.flat (); / / [1, [2,4], 3] arr.flat (Infinity) Thank you for reading this article carefully. I hope the article "how to merge js into arrays" shared by the editor will be helpful to everyone. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!