当前位置:网站首页>[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 .
边栏推荐
- Echart矩形树图:简单实现矩形树图
- 华为开发者认证与DevEco Studio编译器下载
- Leetcode minimum absolute difference of binary search tree simple
- Turn to 2005
- Wechat applet (function transfer parameters, transfer multiple parameters, page Jump)
- 超有范的 logo 在线设计制作工具
- Binary search
- Swift property property
- Multiple reception occurs in the uniapp message delivery
- MySQL custom function
猜你喜欢
杨辉三角形详解
Echart柱状图:堆叠柱状图value格式化显示
Download and installation of universal player potplayer, live stream m3u8 import
The difference between the increment and decrement operators before and after variables i+, +i, I –, – I
Echart柱状图:echart实现堆叠柱状图
AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
js将文本转成语言播放
php 分布式事务 原理详解
【DP之01背包】
Recommend a capacity expansion tool to completely solve the problem of insufficient disk space in Disk C and other disks
随机推荐
php 分布式事务 原理详解
DLL bit by bit
[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib
Leetcode Hamming distance simple
Introduction to USB learning (5) -- looking back, the man was in the dim light
Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
Download and installation of universal player potplayer, live stream m3u8 import
USB status error and its cause (error code)
Leetcode- maximum average of subarray i- simple
Alibaba cloud OSS file download cannot be resumed at a breakpoint
Adding classes dynamically in uni app
Echart histogram: stack histogram value formatted display
Leetcode Timo attack - simple
【DP之01背包】
Leetcode- complement of numbers - simple
js将文本转成语言播放
Turn to 2005
Leetcode- hex number - simple
Security baseline check script - the road to dream
Echart柱状图:堆叠柱状图显示value