repl.txt (1979B)
1 2 {{alias}}( obj, [options,] transform ) 3 Inverts an object, such that keys become values and values become keys, 4 according to a transform function. 5 6 The transform function is provided three arguments: 7 8 - `key`: object key 9 - `value`: object value corresponding to `key` 10 - `obj`: the input object 11 12 The value returned by a transform function should be a value which can be 13 serialized as an object key. Hence, beware when providing objects having 14 values which are themselves objects. The function relies on native object 15 serialization (`#toString`) when converting transform function return values 16 to keys. 17 18 Insertion order is not guaranteed, as object key enumeration is not 19 specified according to the ECMAScript specification. In practice, however, 20 most engines use insertion order to sort an object's keys, thus allowing for 21 deterministic inversion. 22 23 Parameters 24 ---------- 25 obj: ObjectLike 26 Input object. 27 28 options: Object (optional) 29 Options. 30 31 options.duplicates: boolean (optional) 32 Boolean indicating whether to store keys mapped to duplicate values in 33 arrays. Default: `true`. 34 35 transform: Function 36 Transform function. 37 38 Returns 39 ------- 40 out: Object 41 Inverted object. 42 43 Examples 44 -------- 45 // Basic usage: 46 > function transform( key, value ) { return key + value; }; 47 > var obj = { 'a': 'beep', 'b': 'boop' }; 48 > var out = {{alias}}( obj, transform ) 49 { 'abeep': 'a', 'bboop': 'b' } 50 51 // Duplicate values: 52 > function transform( key, value ) { return value; }; 53 > obj = { 'a': 'beep', 'b': 'beep' }; 54 > out = {{alias}}( obj, transform ) 55 { 'beep': [ 'a', 'b' ] } 56 57 // Override duplicate values: 58 > obj = {}; 59 > obj.a = 'beep'; 60 > obj.b = 'boop'; 61 > obj.c = 'beep'; 62 > out = {{alias}}( obj, { 'duplicates': false }, transform ) 63 { 'beep': 'c', 'boop': 'b' } 64 65 See Also 66 -------- 67