I have a Ruby Array:
[
{tags: [1,2]},
{tags: [1,3]},
{tags: [1,4]},
{tags: [2,4]},
{tags: [2,5]}
]
I need to group the objects by their tags:
{
1: [{tags: [1,2]}, {tags: [1,3]}, {tags: [1,4]}],
2: [{tags: [1,2]}, {tags: [2,4]}, {tags: [2,5]}],
3: [{tags: [1,3]}],
4: [{tags: [1,4]}, {tags: [2,4]}],
5: [{tags: [2,5]}]
}
Is there a magic Ruby Way of doing this?
isatisfiesh[:tags].include?[i] => truefor at least one of the five hashesh, theniis a key in the returned hash, and its value is an array of all the hasheshfor whichh[:tags].include?[i] => true. (The next element in the sequence is give by3(n+n^2)/2.)