Stage 3 Draft / May 2, 2022

Array Grouping

1 Scope

This is the spec text of the Array Grouping proposal in ECMAScript.

2 Properties of the Array Prototype Object (22.1.3)

2.1 Array.prototype.groupBy ( callbackfn [ , thisArg ] )

Note 1

callbackfn should be a function that accepts three arguments. groupBy calls callbackfn once for each element in the array, in ascending order, and constructs a new object of arrays. Each value returned by callbackfn is coerced to a property key, and the associated element is included in the array in the constructed object according to this property key.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

groupBy does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by groupBy is set before the first call to callbackfn. Elements which are appended to the array after the call to groupBy begins will not be visited by callbackfn. If existing elements of the array are changed their value as passed to callbackfn will be the value at the time groupBy visits them; elements that are deleted after the call to groupBy begins and before being visited are still visited and are either looked up from the prototype or are undefined.

The return value of groupBy is an object that does not inherit from Object.prototype.

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

  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. If IsCallable(callbackfn) is false, throw a TypeError exception.
  4. Let k be 0.
  5. Let groups be a new empty List.
  6. Repeat, while k < len
    1. Let Pk be ! ToString(𝔽(k)).
    2. Let kValue be ? Get(O, Pk).
    3. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
    4. Perform AddValueToKeyedGroup(groups, propertyKey, kValue).
    5. Set k to k + 1.
  7. Let obj be OrdinaryObjectCreate(null).
  8. For each Record { [[Key]], [[Elements]] } g of groups, do
    1. Let elements be CreateArrayFromList(g.[[Elements]]).
    2. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
  9. Return obj.
Note 2

The groupBy function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

2.2 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] )

Note 1

callbackfn should be a function that accepts three arguments. groupByToMap calls callbackfn once for each element in the array, in ascending order, and constructs a new Map of arrays. Each value returned by callbackfn is used as a key in the Map, and the associated element is included in the array in the constructed Map according to this key.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

groupByToMap does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by groupByToMap is set before the first call to callbackfn. Elements which are appended to the array after the call to groupByToMap begins will not be visited by callbackfn. If existing elements of the array are changed their value as passed to callbackfn will be the value at the time groupByToMap visits them; elements that are deleted after the call to groupByToMap begins and before being visited are still visited and are either looked up from the prototype or are undefined.

The return value of groupByToMap is a Map.

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

  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. If IsCallable(callbackfn) is false, throw a TypeError exception.
  4. Let k be 0.
  5. Let groups be a new empty List.
  6. Repeat, while k < len
    1. Let Pk be ! ToString(𝔽(k)).
    2. Let kValue be ? Get(O, Pk).
    3. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
    4. If key is -0𝔽, set key to +0𝔽.
    5. Perform AddValueToKeyedGroup(groups, key, kValue).
    6. Set k to k + 1.
  7. Let map be ! Construct(%Map%).
  8. For each Record { [[Key]], [[Elements]] } g of groups, do
    1. Let elements be CreateArrayFromList(g.[[Elements]]).
    2. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
    3. Append entry as the last element of map.[[MapData]].
  9. Return map.
Note 2

The groupByToMap function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

2.3 AddValueToKeyedGroup ( groups, key, value )

The abstract operation AddValueToKeyedGroup takes arguments groups (a List of Records that have [[Key]] and [[Elements]] fields), key (an ECMAScript language value), and value (an ECMAScript language value) and returns unused. It performs the following steps when called:

  1. For each Record { [[Key]], [[Elements]] } g of groups, do
    1. If SameValue(g.[[Key]], key) is true, then
      1. Assert: exactly one element of groups meets this criteria.
      2. Append value as the last element of g.[[Elements]].
      3. Return unused.
  2. Let group be the Record { [[Key]]: key, [[Elements]]: « value » }.
  3. Append group as the last element of groups.
  4. Return unused.

2.4 Array.prototype [ @@unscopables ]

The initial value of the @@unscopables data property is an object created by the following steps:

  1. Let unscopableList be OrdinaryObjectCreate(null).
  2. Perform ! CreateDataPropertyOrThrow(unscopableList, "at", true).
  3. Perform ! CreateDataPropertyOrThrow(unscopableList, "copyWithin", true).
  4. Perform ! CreateDataPropertyOrThrow(unscopableList, "entries", true).
  5. Perform ! CreateDataPropertyOrThrow(unscopableList, "fill", true).
  6. Perform ! CreateDataPropertyOrThrow(unscopableList, "find", true).
  7. Perform ! CreateDataPropertyOrThrow(unscopableList, "findIndex", true).
  8. Perform ! CreateDataPropertyOrThrow(unscopableList, "flat", true).
  9. Perform ! CreateDataPropertyOrThrow(unscopableList, "flatMap", true).
  10. Perform ! CreateDataPropertyOrThrow(unscopableList, "groupBy", true).
  11. Perform ! CreateDataPropertyOrThrow(unscopableList, "groupByToMap", true).
  12. Perform ! CreateDataPropertyOrThrow(unscopableList, "includes", true).
  13. Perform ! CreateDataPropertyOrThrow(unscopableList, "keys", true).
  14. Perform ! CreateDataPropertyOrThrow(unscopableList, "values", true).
  15. Return unscopableList.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

Note

The own property names of this object are property names that were not included as standard properties of Array.prototype prior to the ECMAScript 2015 specification. These names are ignored for with statement binding purposes in order to preserve the behaviour of existing code that might use one of these names as a binding in an outer scope that is shadowed by a with statement whose binding object is an Array.

A Copyright & Software License

Copyright Notice

© 2022 Justin Ridgewell

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 https://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.