Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数组扁平化 flat 的几种方式 #556

Open
Chocolate1999 opened this issue Jan 25, 2021 · 6 comments
Open

数组扁平化 flat 的几种方式 #556

Chocolate1999 opened this issue Jan 25, 2021 · 6 comments
Assignees
Labels
JS JS基础与进阶之路 进阶 某类别下进阶问题

Comments

@Chocolate1999
Copy link
Owner

No description provided.

@Chocolate1999 Chocolate1999 added JS JS基础与进阶之路 进阶 某类别下进阶问题 labels Jan 25, 2021
@Chocolate1999 Chocolate1999 self-assigned this Jan 25, 2021
@Chocolate1999
Copy link
Owner Author

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);

@Chocolate1999 Chocolate1999 changed the title 数组展开 flat 的几种方式 数组扁平化flat 的几种方式 Jan 25, 2021
@Chocolate1999 Chocolate1999 changed the title 数组扁平化flat 的几种方式 数组扁平化 flat 的几种方式 Jan 25, 2021
@wxwebfeng
Copy link

第二第三种,不符合吧! 展开后 全部转换为 number类型了,如果是其他类型的数组呢? 这样输出会改变数据类型 或者 NaN

@Chocolate1999
Copy link
Owner Author

第二第三种,不符合吧! 展开后 全部转换为 number类型了,如果是其他类型的数组呢? 这样输出会改变数据类型 或者 NaN

对于当前例子而言采用的就是这几种方式

@garmin954
Copy link

garmin954 commented Feb 22, 2021

/**第六种方式:while+some遍历 */

while(arr.some(Array.isArray)){
     arr =  [].concat(...arr)
}

@123jiacheng123
Copy link

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
}

@maxQQ
Copy link

maxQQ commented Nov 28, 2023

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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JS JS基础与进阶之路 进阶 某类别下进阶问题
Projects
None yet
Development

No branches or pull requests

5 participants