I need to group this YAML strutcture by array elements.
This is example of my YAML structure:
articles:
- title: 'Title 1'
url: 'https://example.com'
data: July 21, 2017
categories:
- 'category 1'
- 'category 2'
- title: 'Title 2'
url: 'https://example.com'
data: July 23, 2017
categories:
- 'category 2'
Result I need is this or similar:
['category 1' =>
[
{title: 'Title 1', url: 'https://example.com', data: July 21, 2017, categories: ['category 1', 'category 2']}
],
'category 2' => [
{title: 'Title 1', url: 'https://example.com', data: July 21, 2017, categories: ['category 1', 'category 2']},
{title: 'Title 2', url: 'https://example.com', data: July 23, 2017, categories: ['category 2']}
]
]
Can you help me? Thank you in advance
UPDATE:
I have tried this:
articles.group_by(&:categories).map{|v, a| [v, a] }
but key is not single category, but categories elements. I think I need a more loop but after some trials I haven't get the result.
Sorry for this, but I newbie for ruby
UPDATE 2: Wrong result is:
[
[
[
"category 1",
"category 2"
],
[
{
"title": "Title 1",
"url": "https://example.com",
"data": "July 21, 2017",
"categories": [
"category 1",
"category 2"
]
}
]
],
[
[
"category 2"
],
[
{
"title": "Title 2",
"url": "https://example.com",
"data": "July 23, 2017",
"categories": [
"category 2"
]
}
]
]
]
[key => value]instead of{ key => value }. Please fix that to avoid confusion.