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

前端 未结 5 1580
情话喂你
情话喂你 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:28

    The flat method is not yet implemented in common browsers (only Chrome v69, Firefox Nightly and Opera 56). It’s an experimental feature. Therefore you cannot use it yet.

    You may want to have your own flat function instead:

    Object.defineProperty(Array.prototype, 'flat', {
        value: function(depth = 1) {
          return this.reduce(function (flat, toFlatten) {
            return flat.concat((Array.isArray(toFlatten) && (depth>1)) ? toFlatten.flat(depth-1) : toFlatten);
          }, []);
        }
    });
    
    console.log(
      [1, [2], [3, [[4]]]].flat(2)
    );

    The code was taken from here by Noah Freitas originally implemented to flatten the array with no depth specified.

    0 讨论(0)
  • 2020-12-16 09:34

    use _.flatten from lodash package ;)

    0 讨论(0)
  • 2020-12-16 09:37

    This can also work.

    let arr = [ [1,2,3], [2,3,4] ];
    console.log([].concat(...arr))
    

    Or for older browsers,

    [].concat.apply([], arr);
    
    0 讨论(0)
  • 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<Object> = new Array<Object>();
    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.

    0 讨论(0)
  • 2020-12-16 09:44

    Array.flat is not supported by your browser. Below are two ways to implement it.

    As a function, the depth variable specifies how deep the input array structure should be flattened (defaults to 1; use Infinity to go as deep as it gets) while the stack is the flattened array, passed by reference on recursive calls and eventually returned.

    function flat(input, depth = 1, stack = [])
    {
        for (let item of input)
        {
            if (item instanceof Array && depth > 0)
            {
                flat(item, depth - 1, stack);
            }
            else {
                stack.push(item);
            }
        }
        
        return stack;
    }
    

    As a Polyfill, extending Array.prototype if you prefer the arr.flat() syntax:

    if (!Array.prototype.flat)
    {
        Object.defineProperty(Array.prototype, 'flat',
        {
            value: function(depth = 1, stack = [])
            {
                for (let item of this)
                {
                    if (item instanceof Array && depth > 0)
                    {
                        item.flat(depth - 1, stack);
                    }
                    else {
                        stack.push(item);
                    }
                }
                
                return stack;
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题