Stage 2 Draft / October 4, 2017

Array.prototype.flatMap & Array.prototype.flatten

Introduction

Array.prototype.flatten returns a new array with all sub-array elements concatted into it recursively up to the specified depth.

Array.prototype.flatMap first maps each element using a mapping function, then flattens the result into a new array. It is identical to a map followed by a flatten of depth 1, but flatMap is quite often useful and merging both into one method is slightly more efficient.

1Array.prototype.flatMap ( callbackfn [ , thisArg ] )

When the flatMap method is called with one or two arguments, the following steps are taken:

  1. Let O be ? ToObject(this value).
  2. If IsCallable(callbackfn) is false, throw a TypeError exception.
  3. If thisArg is present, let T be thisArg; else let T be undefined.
  4. Let sourceLen be ? ToLength(? Get(O, "length")).
  5. Let A be ? ArraySpeciesCreate(O, 0).
  6. Let lastIndex be ? FlattenIntoArray(A, O, O, sourceLen, 0, 1, callbackFn, T).
  7. Perform ? Set(A, "length", lastIndex + 1, true).
  8. Return A.

2Array.prototype.flatten( [ depth ] )

When the flatten method is called with zero or one arguments, the following steps are taken:

  1. Let O be ? ToObject(this value).
  2. Let depthNum be 1.
  3. If depth is not undefined, then
    1. Set depthNum to ? ToInteger(depth).
  4. Let sourceLen be ? ToLength(? Get(O, "length")).
  5. Let A be ? ArraySpeciesCreate(O, 0).
  6. Let lastIndex be ? FlattenIntoArray(A, O, O, sourceLen, 0, depthNum).
  7. Perform ? Set(A, "length", lastIndex + 1, true).
  8. Return A.

3FlattenIntoArray(target, original, source, sourceLen, start, depth [ , mapperFunction, thisArg ])

  1. Let targetIndex be start.
  2. Let sourceIndex be 0.
  3. Repeat, while sourceIndex < sourceLen
    1. Let P be ! ToString(sourceIndex).
    2. Let exists be ? HasProperty(source, P).
    3. If exists is true, then
      1. Let element be ? Get(source, P).
      2. If mapperFunction is present, then
        1. Assert: thisArg is present.
        2. Set element to ? Call(mapperFunction, thisArg , « element, sourceIndex, original »).
      3. Let spreadable be ? IsArray(element).
      4. If spreadable is true and depth > 0, then
        1. Let elementLen be ? ToLength(? Get(element, "length")).
        2. Let nextIndex be ? FlattenIntoArray(target, original, element, elementLen, targetIndex, depth - 1).
        3. Set targetIndex to nextIndex - 1.
      5. Else,
        1. If targetIndex ≥ 253-1, throw a TypeError exception.
        2. Perform ? CreateDataPropertyOrThrow(target, ! ToString(targetIndex), element).
      6. Increase targetIndex by 1.
    4. Increase sourceIndex by 1.
  4. Return targetIndex.

ACopyright & Software License

Copyright Notice

© 2017 Michael Ficarra and Brian Terlson

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.