Gary Sieling

lodash: flatten tuples

For an object like this:


const type_tags = {
  'Academic': ['science'],
  'Tech Conference': [],
  'Documentary': ['documentary'],
  'Historical': []
};

You may want to flatten this in the reverse, which requires the flattening of a list of tuples.

To do this, you first need to create the tuples, reverse them, and flatten them, like so:

_.fromPairs(
  _.flatMap(
    type_tags, 
    (k, v) => 
      k.map( 
        key => [key, v] 
      )
  )
)
Exit mobile version