1

I have an array:

[{
    "name": "Category 1",
    "entries": [{
        "question": "Question 1",
        "answer": "Answer 1"
    }, {
        "question": "Question 2",
        "answer": "Answer 2"
    }, {
        "question": "Question 3",
        "answer": "Answer 3"
    }]
}, {
    "name": "Category 2",
    "entries": [{
        "question": "Question 1",
        "answer": "Answer 1"
    }, {
        "question": "Question 2",
        "answer": "Answer 2"
    }, {
        "question": "Question 3",
        "answer": "Answer 3"
    }]
}]

What I want to do is create a new array of just the entries.

So I end up with this:

[{
    "question": "Question 1",
    "answer": "Answer 1"
}, {
    "question": "Question 2",
    "answer": "Answer 2"
}, {
    "question": "Question 3",
    "answer": "Answer 3"
}, {
    "question": "Question 1",
    "answer": "Answer 1"
}, {
    "question": "Question 2",
    "answer": "Answer 2"
}, {
    "question": "Question 3",
    "answer": "Answer 3"
}]

In PHP I would of just do a push into a new array... but in Ruby is it possible to use group_by or collect to achieve this?

1
  • When you give an example, it's helpful to assign a variable to each input object (e.g., arr = [{ "name": "Category 1",...]). That way, readers can refer to the variable(s) (arr) in answers and comments without having to define it (them), and all readers will use the same variable(s). I suggest you edit your question to do that. Generally there's no need to do that for desired or expected output objects. Commented Sep 15, 2016 at 18:06

2 Answers 2

7

Use Enumerable#flat_map:

array = [{
    "name": "Category 1",
    "entries": [{
        "question": "Question 1",
        "answer": "Answer 1"
    }, {
        "question": "Question 2",
        "answer": "Answer 2"
    }, {
        "question": "Question 3",
        "answer": "Answer 3"
    }]
}, {
    "name": "Category 2",
    "entries": [{
        "question": "Question 1",
        "answer": "Answer 1"
    }, {
        "question": "Question 2",
        "answer": "Answer 2"
    }, {
        "question": "Question 3",
        "answer": "Answer 3"
    }]
}]
array.flat_map { |hash| hash[:entries] }
   #=> [{:question=>"Question 1", :answer=>"Answer 1"},
   #  {:question=>"Question 2", :answer=>"Answer 2"},
   #  {:question=>"Question 3", :answer=>"Answer 3"},
   #  {:question=>"Question 1", :answer=>"Answer 1"},
   #  {:question=>"Question 2", :answer=>"Answer 2"},
   #  {:question=>"Question 3", :answer=>"Answer 3"}]
Sign up to request clarification or add additional context in comments.

5 Comments

Instead of flat_map, map and flatten can be used here to get exactly an array of hashes like array.map {|hash| hash[:entries]}.flatten
@dkp flat_map is about 2 times faster, that map + flatten :)
[[{:question=>"Question 1", :answer=>"Answer 1"}, {:question=>"Question 2", :answer=>"Answer 2"}, {:question=>"Question 3", :answer=>"Answer 3"}], [{:question=>"Question 1", :answer=>"Answer 1"}, {:question=>"Question 2", :answer=>"Answer 2"}, {:question=>"Question 3", :answer=>"Answer 3"}]] - I thought flat_map would result array inside array here
@dkp, re your first comment, that's obvious, considering that flat_map's raison d'être is to avoid the need for map and flatten.
Maybe just say "If array is your array," and delete array, which is a bit distracting.
1

map and flatten would give you desired output:

your_array.map {|hash| hash[:entries]}.flatten

#=> [{:question=>"Question 1", :answer=>"Answer 1"},
# {:question=>"Question 2", :answer=>"Answer 2"},
# {:question=>"Question 3", :answer=>"Answer 3"},
# {:question=>"Question 1", :answer=>"Answer 1"},
# {:question=>"Question 2", :answer=>"Answer 2"},
# {:question=>"Question 3", :answer=>"Answer 3"}]

Comments

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.