Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert one-dimensional array to two-dimensional array in JS

Shulou Source: shulou.com Published: 2022-06-01 00:20:04 09月16日 Update

This article mainly explains "how to convert one-dimensional array and two-dimensional array in JS". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert one-dimensional array and two-dimensional array in JS.

One-dimensional array to two-dimensional array arr1to2 (arr, number) {var arr2 = []; let len = arr.length; for (let I = 0, j = 0; I

< len; i += number, j++) { arr2[j] = arr.splice(0, number); } return arr2; }, 使用: let arr1 = [ 103.743896484375, 21.48374090716327, 101.05224609374999, 20.550508894195637, 103.60107421874999, 17.518344187852218 ]; console.log('一维数组:', arr1); let arr2 = this.arr1to2(arr1, 2); console.log('二维数组:', arr2);

If the second parameter of arr1to2 is 3, the effect is as follows:

From a two-dimensional array to an one-dimensional array

/ / two-dimensional array to one-dimensional array let arr = [103.743896484375, 21.48374090716327], [101.05224609374999,20.5508894195637], [103.60107421874999,17.518344187852218]; console.log ('two-dimensional array:', arr); let newarr = []; for (const item of arr) {newarr = [. Newarr,... item] } console.log ('one-dimensional array:', newarr); at this point, I believe you have a better understanding of "how to convert one-dimensional array and two-dimensional array in JS". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array one-dimensional two-dimensional content learning practical deeper interest parameters practicality practical simple operation effect method more friends websites channels jackers queries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info OPPO Reno Shulou Information Linux