Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report