|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# groupValuesOnKey |
| 22 | + |
| 23 | +> Group the elements of an array according to a specified property name. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var groupValuesOnKey = require( '@stdlib/array/base/group-values-on-key' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### groupValuesOnKey( x, key ) |
| 44 | + |
| 45 | +Groups the elements of an array according to a specified property name. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var x = [ |
| 49 | + { |
| 50 | + 'x': 1, |
| 51 | + 'y': 2 |
| 52 | + }, |
| 53 | + { |
| 54 | + 'x': 1, |
| 55 | + 'y': 3 |
| 56 | + } |
| 57 | +]; |
| 58 | + |
| 59 | +var out = groupValuesOnKey( x, 'y' ); |
| 60 | +// returns { '2': [ { 'x': 1, 'y': 2 } ], '3': [ { 'x': 1, 'y': 3 } ] } |
| 61 | +``` |
| 62 | + |
| 63 | +The function supports the following parameters: |
| 64 | + |
| 65 | +- **x**: input array. |
| 66 | +- **key**: property name whose values are used to determine groups. |
| 67 | + |
| 68 | +</section> |
| 69 | + |
| 70 | +<!-- /.usage --> |
| 71 | + |
| 72 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 73 | + |
| 74 | +<section class="notes"> |
| 75 | + |
| 76 | +## Notes |
| 77 | + |
| 78 | +- Each value associated with a specified `key` should resolve to a value which can be serialized as an object key. As a counterexample, |
| 79 | + |
| 80 | + ```javascript |
| 81 | + var x = [ |
| 82 | + { |
| 83 | + 'x': 1, |
| 84 | + 'y': {} |
| 85 | + }, |
| 86 | + { |
| 87 | + 'x': 1, |
| 88 | + 'y': {} |
| 89 | + } |
| 90 | + ]; |
| 91 | + |
| 92 | + var out = groupValuesOnKey( x, 'y' ); |
| 93 | + // returns { '[object Object]': [ { 'x': 1, 'y': {} }, { 'x': 1, 'y': {} } ] } |
| 94 | + ``` |
| 95 | + |
| 96 | + while each "group" is unique, all input array elements resolve to the same group because each group identifier serializes to the same string. |
| 97 | + |
| 98 | +- If provided an empty array, the function returns an empty object. |
| 99 | + |
| 100 | +</section> |
| 101 | + |
| 102 | +<!-- /.notes --> |
| 103 | + |
| 104 | +<!-- Package usage examples. --> |
| 105 | + |
| 106 | +<section class="examples"> |
| 107 | + |
| 108 | +## Examples |
| 109 | + |
| 110 | +<!-- eslint no-undef: "error" --> |
| 111 | + |
| 112 | +```javascript |
| 113 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 114 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 115 | +var groupValuesOnKey = require( '@stdlib/array/base/group-values-on-key' ); |
| 116 | + |
| 117 | +function clbk( idx ) { |
| 118 | + return { |
| 119 | + 'x': idx, |
| 120 | + 'y': discreteUniform( 0, 10 ) |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +// Create an array of random objects: |
| 125 | +var values = filledBy( 100, clbk ); |
| 126 | + |
| 127 | +// Group elements according to the values of `y`: |
| 128 | +var out = groupValuesOnKey( values, 'y' ); |
| 129 | +// returns {...} |
| 130 | + |
| 131 | +console.log( out ); |
| 132 | +``` |
| 133 | + |
| 134 | +</section> |
| 135 | + |
| 136 | +<!-- /.examples --> |
| 137 | + |
| 138 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 139 | + |
| 140 | +<section class="references"> |
| 141 | + |
| 142 | +</section> |
| 143 | + |
| 144 | +<!-- /.references --> |
| 145 | + |
| 146 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 147 | + |
| 148 | +<section class="related"> |
| 149 | + |
| 150 | +</section> |
| 151 | + |
| 152 | +<!-- /.related --> |
| 153 | + |
| 154 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 155 | + |
| 156 | +<section class="links"> |
| 157 | + |
| 158 | +</section> |
| 159 | + |
| 160 | +<!-- /.links --> |
0 commit comments