repl.txt (2063B)
1 2 {{alias}}( obj, [options,] indicator ) 3 Group values according to an indicator function. 4 5 When invoked, the indicator function is provided two arguments: 6 7 - `value`: object value 8 - `key`: object key 9 10 The value returned by an indicator function should be a value which can be 11 serialized as an object key. 12 13 If provided an empty object with no prototype, the function returns an empty 14 object. 15 16 The function iterates over an object's own and inherited properties. 17 18 Key iteration order is *not* guaranteed, and, thus, result order is *not* 19 guaranteed. 20 21 Parameters 22 ---------- 23 obj: Object|Array|TypedArray 24 Input object to group. 25 26 options: Object (optional) 27 Options. 28 29 options.thisArg: any (optional) 30 Execution context. 31 32 options.returns: string (optional) 33 If `values`, values are returned; if `keys`, keys are returned; if `*`, 34 both keys and values are returned. Default: 'values'. 35 36 indicator: Function 37 Indicator function indicating which group a value in the input object 38 belongs to. 39 40 Returns 41 ------- 42 out: Object 43 Group results. 44 45 Examples 46 -------- 47 > function indicator( v ) { 48 ... if ( v[ 0 ] === 'b' ) { 49 ... return 'b'; 50 ... } 51 ... return 'other'; 52 ... }; 53 > function Foo() { this.a = 'beep'; this.b = 'boop'; return this; }; 54 > Foo.prototype = Object.create( null ); 55 > Foo.prototype.c = 'foo'; 56 > Foo.prototype.d = 'bar'; 57 > var obj = new Foo(); 58 > var out = {{alias}}( obj, indicator ) 59 { 'b': [ 'beep', 'boop', 'bar' ], 'other': [ 'foo' ] } 60 61 // Output group results as keys: 62 > var opts = { 'returns': 'keys' }; 63 > out = {{alias}}( obj, opts, indicator ) 64 { 'b': [ 'a', 'b', 'd' ], 'other': [ 'c' ] } 65 66 // Output group results as key-value pairs: 67 > opts = { 'returns': '*' }; 68 > out = {{alias}}( obj, opts, indicator ) 69 { 'b': [['a','beep'], ['b','boop'], ['d','bar']], 'other': [['c','foo' ]] } 70 71 See Also 72 -------- 73