当前位置:网站首页>[JS] array flattening
[JS] array flattening
2022-06-13 06:14:00 【A strong foam】
Concept
Array flattening refers to changing a multidimensional array into a one-dimensional array
[1, [2, 3, [4, 5]]] ------> [1, 2, 3, 4, 5]
Realization
1. Extension operator
es6 The extension operator can change a two-dimensional array into one-dimensional
[].concat(...[1, 2, 3, [4, 5]]); // [1, 2, 3, 4, 5]
Based on this result, we can do a traversal , if arr If there is an array in, use the one-time extension operator , Until there is no .
function flatten(arr) {
while(arr.some(item=>Array.isArray(item))) {
arr = [].concat(...arr);
}
return arr;
}
console.log(flatten([1, [2, 3, [4, 5]]])) // [1, 2, 3, 4, 5]
2. reduce
Traverse every item in the array , If the value is an array, recursively traverse , otherwise concat.
function flatten(arr) {
return arr.reduce((result, item)=> {
return result.concat(Array.isArray(item) ? flatten(item) : item);
}, []);
}
3. recursive
Recursively traverse every item , If it's an array, continue traversing , otherwise concat.
function flatten(arr) {
var res = [];
arr.map(item => {
if(Array.isArray(item)) {
res = res.concat(flatten(item));
} else {
res.push(item);
}
});
return res;
}
4. toString & split
Call array toString Method , Change the array to character string then Reuse split Division Revert to array .
function flatten(arr) {
return arr.toString().split(',').map(function(item) {
return Number(item);
})
}
console.log(flatten([1, [2, 3, [4, 5]]])) // [1, 2, 3, 4, 5]
Add :
console.log([1, [2, 3, [4, 5]]].toString()) // 1,2,3,4,5
5. join & split
And the above toString equally ,join You can also convert arrays to strings .
function flatten(arr) {
return arr.join(',').split(',').map(function(item) {
return parseInt(item);
})
}
summary
The core :
(1) Traversal array arr, if arr[i] For arrays, recursively traverse , until arr[i] Not for arrays and then with previous results concat.
(2) Convert to string , Reprocessing .
(3) Extension operator .
边栏推荐
- A glimpse of [wechat applet]
- 本地文件秒搜工具 Everything
- Minimum spanning tree (prim+kruskal) learning notes (template +oj topic)
- Leetcode minimum absolute difference of binary search tree simple
- php redis 制作高迸发秒杀
- Custom view - extensible collapsexpendview
- The boys x pubgmobile linkage is coming! Check out the latest game posters
- Leetcode- minimum number of operations to make array elements equal - simple
- USB status error and its cause (error code)
- Wechat applet (get location)
猜你喜欢

JVM基础

Custom view - extensible collapsexpendview

万能播放器 PotPlayer 的下载与安装,直播流 m3u8 导入

Echart rectangular tree diagram: simple implementation of rectangular tree diagram

【ONE·Data || 带头双向循环链表简单实现】

Hbuilderx: installation of hbuilderx and its common plug-ins

USB debugging assistant (20191028)

本地文件秒搜工具 Everything
![A glimpse of [wechat applet]](/img/ef/17564759e720b06c2bd496d1db7544.jpg)
A glimpse of [wechat applet]

Detailed explanation of PHP distributed transaction principle
随机推荐
Commit specification
【ONE·Data || 带头双向循环链表简单实现】
The technical analysis of ERP systems of the two camps in the world has been picked up many times.
Audio stereo to mono (Audio Dual Channel to mono channel)
After clicking the uniapp e-commerce H5 embedded applet, the page prompts "the page iframe does not support referencing non business domain names"
CORS request principle
Wechat applet (function transfer parameters, transfer multiple parameters, page Jump)
推荐扩容工具,彻底解决C盘及其它磁盘空间不够的难题
Annotation only integration SSM framework
Wechat applet: use of global state variables
Echart柱状图:堆叠柱状图value格式化显示
微信小程序:全局状态变量的使用
You still can't remotely debug idea? Come and have a look at my article. It's easy to use
USB debugging assistant
Leetcode- longest continuous increasing sequence - simple
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
[written examination questions of meituan]
Turn to 2005
Multiple reception occurs in the uniapp message delivery
Custom view - extensible collapsexpendview