Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert a two-dimensional array to an one-dimensional array by vue

Shulou Source: shulou.com Published: 2022-05-31 19:51:16 09月10日 Update

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

Converting a two-dimensional array to a one-dimensional array requires

Array nested data, resulting in loop inconvenience, thinking of merging two-dimensional arrays into one-dimensional data, convenient data operation

specific implementation

Using the reduce method

reduce: Returns a new array without changing the original array. The first parameter is the previous return value, the second parameter is the next array element, the first and second parameters are array[0], array[1] respectively;

let flat=[[1,2,3],[4,5,6],[6,7,8]].reduce( (prev,next)=> { return prev.concat(next);//loop concatenates arrays});console.log(flat);

[1,[2, 3, 4, 5] var arr =[1,[[2, 3, 4, 5];var newArr =[];function Arr(arr){ arr.map(item=>{ if(Array.isArray(item)){ Arr(item) }else{ newArr.push(item) } }) return newArr}console.log(Arr(arr));//[ 1, 2, 3, 4, 5 ]reduce +concat + recursion var arr=[[[2,3],4]],5];function concatArr(arr){ var newArr=arr.reduce((pre,next)=>{ return pre.concat(Array.isArray(next)? concatArr(next):next) },[]) return newArr;}console.log (concatArr(arr));//[ 2, 3, 4, 5 ]join+splitvar arr=[[1,2],3,[4,[5]]];var arr1=arr.join().split(',');console.log(arr1);//["1", "2", "3", "4", "5"]toString+splitvar arr=[[1,2],3,[4,[5]]]; var arr1=arr.toString().split(',');console.log(arr1);//["1", "2", "3", "4", "5"]evalvar arr=[[1,2],3,[4,[5]]];var arr1=eval ('[' + arr + ']');console.log(arr1);//[ 1, 2, 3, 4, 5 ] At this point, I believe that everyone has a deeper understanding of "how vue converts two-dimensional arrays into one-dimensional arrays." Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Arrays one-dimensional two-dimensional methods parameters data elements content recursion learning looping practical deeper ab initio interest practicality practicality that is simple operation time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info vpn Linux MySQL