flatMap 实际上是先 map 再 flat

    1. // 扁平化一层
    2. Array.prototype.myFlat=function(){
    3. return this.reduce((sum,cur)=>sum.concat(cur),[])
    4. }
    5. Array.prototype.myFlatMap = function (mapper) {
    6. return this.map(mapper).myFlat();
    7. };
    8. console.log(["it's Sunny in", "", "California"].myFlatMap(x => x.split(" ")));
    9. // ['it's', 'Sunny', 'in', '', 'California']
    10. console.log([1, 2, [3], 4].flatMap((x) => x + 1));
    11. console.log([1, 2, [3], 4].myFlatMap((x) => x + 1));
    12. // [2, 3, '31', 5]