Returns a value for an object element that can be serialized as an object key.
Returns a value for an object element that can be serialized as an object key.
Returns a value for an object element that can be serialized as an object key.
Returns a value for an object element that can be serialized as an object key.
Inverts an object, such that keys become values and values become keys, according to a transform function.
The transform function is provided three arguments:
key
: object keyvalue
: object value corresponding to key
obj
: the input objectThe value returned by a transform function should be a value which can be serialized as an object key. Hence, beware when providing objects having values which are themselves objects. The function relies on native object serialization (#toString
) when converting transform function return values to keys.
Insertion order is not guaranteed, as object key enumeration is not specified according to the ECMAScript specification. In practice, however, most engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.
input object
transform function
inverted object
function transform( key, value ) {
return key + value;
}
var obj = {
'a': 'beep',
'b': 'boop'
};
var out = invertBy( obj, transform );
// returns { 'abeep': 'a', 'bboop': 'b' }
function transform( key, value ) {
return value;
}
var obj = {
'a': 'beep',
'b': 'beep'
};
var out = invertBy( obj, transform );
// returns { 'beep': [ 'a', 'b' ] }
Inverts an object, such that keys become values and values become keys, according to a transform function.
The transform function is provided three arguments:
key
: object keyvalue
: object value corresponding to key
obj
: the input objectThe value returned by a transform function should be a value which can be serialized as an object key. Hence, beware when providing objects having values which are themselves objects. The function relies on native object serialization (#toString
) when converting transform function return values to keys.
Insertion order is not guaranteed, as object key enumeration is not specified according to the ECMAScript specification. In practice, however, most engines use insertion order to sort an object's keys, thus allowing for deterministic inversion.
inverted object
function transform( key, value ) {
return value;
}
var obj = {};
obj.a = 'beep';
obj.b = 'boop';
obj.c = 'beep'; // inserted after `a`
var opts = {
'duplicates': false
};
var out = invertBy( obj, opts, transform );
// returns { 'beep': 'c', 'boop': 'b' }
Returns a value for an object element that can be serialized as an object key.
object key
object value corresponding to
key
key in inverted object