Comments (6)
let arr = [1, 2, [3, 4], [5, 6, [7, 8, 9]]];
/**第一种方式:flat */
let res1 = arr.flat(Infinity);
console.log(res1);
/**第二种方式:join + split*/
let res2 = arr.join().split(',').map(Number);
console.log(res2);
/**第三种方式: toString + split*/
let res3 = arr.toString().split(',').map(Number);
console.log(res3);
/**第四种方式:递归展开 */
const flattern = arr=>{
const res = [];
arr.forEach((item)=>{
if(Array.isArray(item)){
res.push(...flattern(item));
}else{
res.push(item);
}
})
return res;
}
flattern(arr);
/**第五种方式:递归concat */
function flattern2(arr){
return [].concat(
...arr.map(item=>Array.isArray(item)? flattern2(item):item)
)
}
flattern2(arr);
from front-end-learning-to-organize-notes.
第二第三种,不符合吧! 展开后 全部转换为 number类型了,如果是其他类型的数组呢? 这样输出会改变数据类型 或者 NaN
from front-end-learning-to-organize-notes.
第二第三种,不符合吧! 展开后 全部转换为 number类型了,如果是其他类型的数组呢? 这样输出会改变数据类型 或者 NaN
对于当前例子而言采用的就是这几种方式
from front-end-learning-to-organize-notes.
/**第六种方式:while+some遍历 */
while(arr.some(Array.isArray)){
arr = [].concat(...arr)
}
from front-end-learning-to-organize-notes.
function flatFun(arr, depth = 1) {
let count = 0;
let result = [];
const flatMap = (arr) => {
arr.map((item, index, array) => {
if (Array.isArray(item)) {
if (count < depth) {
count++;
flatMap(item);
} else {
result.push(item);
}
} else {
result.push(item);
if (index === array.length - 1) num = 0
}
});
};
flatMap(arr)
return result
}
from front-end-learning-to-organize-notes.
No description provided.
function flatFun(arr, depth = 1) { let count = 0; let result = []; const flatMap = (arr) => { arr.map((item, index, array) => { if (Array.isArray(item)) { if (count < depth) { count++; flatMap(item); } else { result.push(item); } } else { result.push(item); if (index === array.length - 1) num = 0 } }); }; flatMap(arr) return result }
from front-end-learning-to-organize-notes.
Related Issues (20)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from front-end-learning-to-organize-notes.