Get the App
SLTechnology News&Howtos  ›  Development  › 

How does JavaScript tile a multidimensional array

Shulou Source: shulou.com Published: 2022-06-01 04:58:27 10月26日 Update

This article will explain in detail how to tile multi-dimensional arrays in JavaScript. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Tiled multidimensional array

Using the Spread operation, you can easily tile nested multidimensional arrays:

Const arr = [11, [22, 33], [44, 55], 66]; const flatArr = [] .concat (.arr); / / = > [11, 22, 33, 44, 55, 66]

Unfortunately, the above method only applies to two-dimensional arrays. However, through recursion, we can tile nested arrays of any dimension.

Unction flattenArray (arr) {const flattened = [] .concat (.. arr); return flattened.some (item = > Array.isArray (item))? FlattenArray (flattened): flattened;} const arr = [11, [22,33], [44, [55,66, [77, [88]], 99]; const flatArr = flattenArray (arr) / / = > [11, 22, 33, 44, 55, 66, 77, 88, 99] this is the end of the article on "how to tile multidimensional arrays in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Array multidimensional article more good practical content article method knowledge dimension faceted two-dimensional recursive reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info vpn Shulou Technology Redmi