ruby - Merge hash with array values to one array -
how merge hash array values 1 array:
h = { one: ["one1", "one2"], two: ["two1", "two2"] } after merge should be:
["one1","one2","two1","two2"]
h.values.flatten # => ["one1", "one2", "two1", "two2"] you can same keys, of course. reason need flatten here because values arrays, h.values alone return [["one1", "one2"], ["two1", "two2"]].
also, fyi, merge means different (and pretty useful) in ruby.
if want make sure flattens 1 level (per @tokland's comment), can provide optional argument flatten such flatten(1).
Comments
Post a Comment