Given a hash of arrays, how to create an array of hashes with each possible combo

Eddie Prislac

Given a hash with values containing arrays of unequal lengths,

{a: [1, 2, 3], b: [1, 2], c: [1]}

is it possible to create an array of hashes containing the same keys, of all permutations of single values like the following without resorting to nesting multiple loops?

[
  {a: 1, b: 1, c: 1},
  {a: 1, b: 2, c: 1},
  {a: 2, b: 1, c: 1},
  {a: 2, b: 2, c: 1},
  {a: 3, b: 2, c: 1},
  {a: 3, b: 2, c: 1}
]

We accomplish this using nested each loops for each key, but this looks nasty at a large scale. Actual data contain many more keys.

Marcin Kołodziej
keys = hash.keys
hash.values.inject(:product).map do |p|
  Hash[keys.zip(p.flatten)]
end

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to count items in arrays of values in an array of hashes

分類Dev

How to convert array of hashes into a single hash and count repeated items?

分類Dev

How to group array of hashes and create nested array after keyword?

分類Dev

Sort array of arrays that is a value of a hash

分類Dev

From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X

分類Dev

Sort array of arrays by array value into hash in ruby

分類Dev

How to create an array of binary digits of given unsigned integer numbers with Numpy?

分類Dev

How to create a two dimensional array of given size in C++

分類Dev

Ruby sort hash of hashes

分類Dev

How to get list of git commit hashes that start with given string

分類Dev

Possible to create a array of referencetype?

分類Dev

How To Create Array of Arrays? [ ["a","b","c"] , ["c","d","e"] ]

分類Dev

Create nested hashes from an array of elements for JSON encoding

分類Dev

Create all possible permutations of given variable set

分類Dev

Dealing with hashes inside arrays

分類Dev

Adding to and deleting hashes in arrays

分類Dev

Hashes in hashes that produce default array

分類Dev

Given a set of letters, how to create all possible combinations of a word with these letters, duplicated with a specified number?

分類Dev

hash() function producing inconsistent hashes

分類Dev

How to create user password hash

分類Dev

How to compare an array to an array of arrays?

分類Dev

How to insert an array into an array of arrays?

分類Dev

How to generate all possible combinations of given length

分類Dev

Is it possible to re-create a lost account with given homedir and UID in macOS?

分類Dev

Create strongly typed array of arrays in TypeScript

分類Dev

Create an Array with a KEY if values exists in two arrays?

分類Dev

Is it possible to constrain the values of an array with Typescript to a given subset of string values?

分類Dev

syntax for accessing hash param in hash of hashes when using foreach

分類Dev

How to convert an array of arrays into a matrix?

Related 関連記事

  1. 1

    How to count items in arrays of values in an array of hashes

  2. 2

    How to convert array of hashes into a single hash and count repeated items?

  3. 3

    How to group array of hashes and create nested array after keyword?

  4. 4

    Sort array of arrays that is a value of a hash

  5. 5

    From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X

  6. 6

    Sort array of arrays by array value into hash in ruby

  7. 7

    How to create an array of binary digits of given unsigned integer numbers with Numpy?

  8. 8

    How to create a two dimensional array of given size in C++

  9. 9

    Ruby sort hash of hashes

  10. 10

    How to get list of git commit hashes that start with given string

  11. 11

    Possible to create a array of referencetype?

  12. 12

    How To Create Array of Arrays? [ ["a","b","c"] , ["c","d","e"] ]

  13. 13

    Create nested hashes from an array of elements for JSON encoding

  14. 14

    Create all possible permutations of given variable set

  15. 15

    Dealing with hashes inside arrays

  16. 16

    Adding to and deleting hashes in arrays

  17. 17

    Hashes in hashes that produce default array

  18. 18

    Given a set of letters, how to create all possible combinations of a word with these letters, duplicated with a specified number?

  19. 19

    hash() function producing inconsistent hashes

  20. 20

    How to create user password hash

  21. 21

    How to compare an array to an array of arrays?

  22. 22

    How to insert an array into an array of arrays?

  23. 23

    How to generate all possible combinations of given length

  24. 24

    Is it possible to re-create a lost account with given homedir and UID in macOS?

  25. 25

    Create strongly typed array of arrays in TypeScript

  26. 26

    Create an Array with a KEY if values exists in two arrays?

  27. 27

    Is it possible to constrain the values of an array with Typescript to a given subset of string values?

  28. 28

    syntax for accessing hash param in hash of hashes when using foreach

  29. 29

    How to convert an array of arrays into a matrix?

ホットタグ

アーカイブ