0

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"
        ]
      }
    ]
  ]
]
6
  • 1
    Where are you stuck? Commented Sep 19, 2017 at 8:52
  • I have updated my question. Thank you Commented Sep 19, 2017 at 9:01
  • is the desired output correct? should category 2 be a subelement of category 1 ? Commented Sep 19, 2017 at 9:08
  • "but key is not single category" – could you show the (wrong) result, please? Commented Sep 19, 2017 at 9:10
  • BTW, your expected result seems to be a hash, but is expressed as an array literal, i.e. you are using [key => value] instead of { key => value }. Please fix that to avoid confusion. Commented Sep 19, 2017 at 9:12

2 Answers 2

2

It seems, in this case, the plain old good reducer would suit your needs better:

articles.each_with_object({}) do |article, acc|
  acc.merge! article.categories.map { |c| [c, article] }.to_h
end

or even:

articles.map do |article|
  article.categories.map { |c| [c, article] }.to_h
end.reduce(&:merge)
Sign up to request clarification or add additional context in comments.

Comments

1

Do you need a group_by? Why not an easy nested loop?

result = {}
articels.each do |article|
  article.categories.each do |cat|
    result[cat] ||= []
    result[cat] << article
  end
end

2 Comments

You are abusing each with preliminary local variable. Please use each_with_object instead.
@mudasobwa that's true, it was more a statement, that he should have be able to solve this with easy methods. It was not the idea to show the best way. Anyway your solution is quite good, always glad to read something like this ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.