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] 
      )
  )
)

Leave a Reply

Your email address will not be published. Required fields are marked *