.flat() is not a function, what's wrong?

前端 未结 5 1619
情话喂你
情话喂你 2020-12-16 08:40

The following code

function steamrollArray(arr) {
  // I\'m a steamroller, baby
  return arr.flat();
}

steamrollArray([1, [2], [3, [[4]]]]);
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 09:41

    Not sure if it is a valid answer however in my attemp to flat an array I employed the destructuring_assignment introduced in ES6.

    // typeScriptArray:Array = new Array();
    let concatArray = [];
    
    let firstArray = [1,2,3];
    let secondArray = [2,3,4];
    
    concatArray.push(...firstArray);
    concatArray.push(...secondArray);
    
    console.log(concatArray);
    
    
    

    It works like a charm even though I'm not sure if any broswer compatibily issues may arise.

    提交回复
    热议问题