Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I can simplify? @Flambino's solutionsolution a bit with _.merge:

I can simplify? @Flambino's solution a bit with _.merge:

I can simplify? @Flambino's solution a bit with _.merge:

notes on reduce
Source Link

Analyses of solutions

reduce

I can simplify? @Flambino's solution a bit with _.merge:

function arrayAwareInvert(obj) {
  return _.reduce(obj, function (result, values, key) {
    return _.merge(result, 
                   _.mapValues(_.object(values), function(v) { return key; }));
  }, {});
}

I like using merge here because it describes what's happening very well. But I don't really like how much code is needed to combine a vector and a string into an object hash with keys as elements of the vector and values as the string:

_.mapValues(_.object(values), function(v) { return key; })

As one (of many) alternatives, you could do this instead:

_.object(values, values.map(function(v) { return key; }))

but both of these seem obfuscated, compared with how clear the reduce and merge steps are.

Analyses of solutions

reduce

I can simplify? @Flambino's solution a bit with _.merge:

function arrayAwareInvert(obj) {
  return _.reduce(obj, function (result, values, key) {
    return _.merge(result, 
                   _.mapValues(_.object(values), function(v) { return key; }));
  }, {});
}

I like using merge here because it describes what's happening very well. But I don't really like how much code is needed to combine a vector and a string into an object hash with keys as elements of the vector and values as the string:

_.mapValues(_.object(values), function(v) { return key; })

As one (of many) alternatives, you could do this instead:

_.object(values, values.map(function(v) { return key; }))

but both of these seem obfuscated, compared with how clear the reduce and merge steps are.

more underscore-friendly verbiage
Source Link

I frequently work with object hashes whose values are one-dimensional arrays (vectors). I've written a function using lodash/underscore (NB. only tested with lodash) that performs an array-aware invert, whose output object has keys that are the unique elements of the input object's value vectors, and values that are the input object's keys.

(NB. I can't use lodash'slodash/underscore's built-in invert with an object hash with array-valued keys because the resulting object has stringified keys which simply stringify the arrays—an output object key might be [1, 2, 3].toString(), and entirely useless.)

Lodash's flatten is used to remove one level of nesting, i.e., transform [[[k1, v1]], [[k2, v2], [k3, v3]]] to [[k1, v1], [k2, v2], [k3, v3]].

Finally lodash's object is called to convert this list of 2-tuple key-value pairs into an object hash.

I frequently work with object hashes whose values are one-dimensional arrays (vectors). I've written a function using lodash that performs an array-aware invert, whose output object has keys that are the unique elements of the input object's value vectors, and values that are the input object's keys.

(NB. I can't use lodash's built-in invert with an object hash with array-valued keys because the resulting object has stringified keys.)

Lodash's flatten is used to remove one level of nesting, i.e., transform [[[k1, v1]], [[k2, v2], [k3, v3]]] to [[k1, v1], [k2, v2], [k3, v3]].

Finally lodash's object is called to convert this list of 2-tuple key-value pairs into an object hash.

I frequently work with object hashes whose values are one-dimensional arrays (vectors). I've written a function using lodash/underscore (NB. only tested with lodash) that performs an array-aware invert, whose output object has keys that are the unique elements of the input object's value vectors, and values that are the input object's keys.

(NB. I can't use lodash/underscore's built-in invert with an object hash with array-valued keys because the resulting object has keys which simply stringify the arrays—an output object key might be [1, 2, 3].toString(), and entirely useless.)

flatten is used to remove one level of nesting, i.e., transform [[[k1, v1]], [[k2, v2], [k3, v3]]] to [[k1, v1], [k2, v2], [k3, v3]].

Finally object is called to convert this list of 2-tuple key-value pairs into an object hash.

added link to underscore documentation
Source Link
Loading
Source Link
Loading