Dimensionality reduction of two-dimensional arrayDimensionality reduction using array instance method concat and ES6 spread operator let arr=[1,2,[3,4],5]; let arr1=[].concat(...arr); //First use the spread operator to break up the outermost array. concat has its own function to break up the glued arrays. console.log(arr1); // [1, 2, 3, 4, 5] //Compatibility with ES6 before using apply to break up data var arr2=[1,2,[3,4],5]; var arr3=[].concat.apply([],arr2); console.log(arr3); // [1, 2, 3, 4, 5] Dimensionality reduction of multidimensional arraysRecursive Dimensionality Reduction //Several ways to use array function recursive dimensionality reduction //some&concat //Use some to check if the array contains an array and then call itself to reduce the dimension function fun(arr){ arr = [].concat (...arr); // Check if the reduced array still contains sub-arrays let hasArray = arr.some(function(elem){ return Array.isArray(elem); }) if(hasArray){ //If it contains a subarray arr=fun(arr); //Then it can only reduce the dimension once more until it is checked that it no longer contains a subarray} return arr; }; //forEach&instanceof //Judge each element of the array to see if the array is continuing to call itself, rather than putting it into the empty array prepared in advance function fun2(arr){ let ret = []; let toArr = function(arr){ arr.forEach(function(item){ item instanceof Array ? toArr(item) : ret.push(item); }); } toArr(arr); return ret; } //reduce&concat //Finally prepare an empty array and check if each item in the passed array is an array. If it is, call itself again. If not, use concat to summarize it into an empty array. function fun3(sarr){ return sarr.reduce((pre,val)=>{ return Array.isArray(val) ? pre.concat(fun3(val)): pre.concat(val) },[]) } Array.prototype.flat() ES10 adds Array.prototype.flat(), which is used to break up nested arrays into one-dimensional arrays. This method returns a new array and has no effect on the original data. var arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] var arr2 = [1, 2, [3, 4, [5, 6]]]; arr2.flat(); // [1, 2, 3, 4, [5, 6]] var arr3 = [1, 2, [3, 4, [5, 6]]]; arr3.flat(2); // [1, 2, 3, 4, 5, 6] //Use Infinity as the depth to expand nested arrays of arbitrary depth arr3.flat(Infinity); // [1, 2, 3, 4, 5, 6] This concludes this article on several methods of JS array dimensionality reduction. For more relevant JS array dimensionality reduction content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Summary of Problems in Installation and Usage of MySQL 5.7.19 Winx64 ZIP Archive
>>: How to use cutecom for serial communication in Ubuntu virtual machine
Table of contents 1. Communication method between...
A few days ago, I exchanged some knowledge about ...
Table of contents Preface 1. Understanding with e...
echarts component official website address: https...
Special statement: This article is translated bas...
Table of contents Preface What is a virtual list?...
Table of contents 1. The role of array: 2. Defini...
I won't go into details about how important b...
For work needs, I need to make a mobile phone adap...
Table of contents 1. Introduction 2. Component De...
This article introduces how to install the system...
Table of contents Linux MySQL 5.5 upgraded to MyS...
Use the following terminal command to install the...
Table of contents 1. Preparation 2. Define the gl...
Preface Recently, I found a pitfall in upgrading ...