Git Product home page Git Product logo

Comments (6)

Chocolate1999 avatar Chocolate1999 commented on August 23, 2024
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.

wxwebfeng avatar wxwebfeng commented on August 23, 2024

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

from front-end-learning-to-organize-notes.

Chocolate1999 avatar Chocolate1999 commented on August 23, 2024

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

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

from front-end-learning-to-organize-notes.

garmin954 avatar garmin954 commented on August 23, 2024

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

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

from front-end-learning-to-organize-notes.

123jiacheng123 avatar 123jiacheng123 commented on August 23, 2024

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.

maxQQ avatar maxQQ commented on August 23, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.