一维数组转二维数组
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);
如果arr1to2第二个参数为3,效果如下:
二维数组转一维数组
//二维数组转一维数组 let arr = [ [103.743896484375, 21.48374090716327], [101.05224609374999, 20.550508894195637], [103.60107421874999, 17.518344187852218] ]; console.log('二维数组:', arr); let newarr = []; for (const item of arr) { newarr = [...newarr, ...item]; } console.log('一维数组:', newarr);
到此这篇关于js中一维数组和二维数组互转的文章就介绍到这了,更多相关js数组互转内容请搜索阿兔在线工具以前的文章或继续浏览下面的相关文章希望大家以后多多支持阿兔在线工具!