-5

Sorry for the bad English Title but the code below would make more sense. I used .each to get some keys and values that I wanted from the data but when I do it I get this result.

[
  [
    {
        id: 1,
        name: "Immad",
        age: 18
    }
  ],
  [
    {
        id: 2,
        name: "Vicky",
        age: 21
    }
  ],
  [
    {
        id: 3,
        name: "Adam",
        age: 24
    }
  ]
]

I want to map the objects to some js library for which I want it to be like:

[
    {
        id: 1,
        name: "Immad",
        age: 18
    },
    {
        id: 2,
        name: "Vicky",
        age: 21
    },
    {
        id: 3,
        name: "Adam",
        age: 24
    }
]

Can anybody please atleast give me hint what should I use in order to do it using ruby. Thanks in advance.

Immad Hamid
  • 789
  • 1
  • 8
  • 21
  • "I used .each to get some keys and values that I wanted from the data but when I do it I get this result." 1. Please edit your question to include the code you've written so far. 2. What data? – Jordan Running Nov 06 '17 at 20:45
  • I have googled it but unable to find any answer on it that solves this, even if someone has answered and you know then please share the link. thanks – Immad Hamid Nov 06 '17 at 20:46
  • If you're not willing to edit your question to include the code you've written so far and some example data, you're not going to get much help here. Please read my comment again and edit your question accordingly. – Jordan Running Nov 06 '17 at 20:48
  • @jordan Running I thought this might be enough to get help. Well this is the code that I wrote to fetch the title, user name, training schedule date and enrollment url enrollment_json = enrollments.map {|e| ["title" => "##{e.id} - #{e.user.full_name}", "id" => e.id, "start" => e.training_schedule_date, "end" => e.training_schedule_date, "url" => "#{enrollment_url(e.id)}"] } – Immad Hamid Nov 06 '17 at 20:57
  • and @anothermh has given the right answer and I was able to fix the code – Immad Hamid Nov 06 '17 at 20:58
  • 3
    Tip: `["title" => ... ]` is not the same as `{ "title" => ... }`. – Jordan Running Nov 06 '17 at 20:59
  • Thanks Jordan this should be it. I should have directly used {} – Immad Hamid Nov 08 '17 at 09:52

3 Answers3

2

There already is an Array method.

foo_array.flatten!

or non destructive (just a return value foo_array remains unchanged)

foor_array.flatten
smile2day
  • 1,585
  • 1
  • 24
  • 34
1

Simple, You should do like this

Test = [[{:id=>1, :name=>"Immad", :age=>18}], [{:id=>2, :name=>"Vicky", :age=>21}], [{:id=>3, :name=>"Adam", :age=>24}]]

Use .flatten here.

Test.flatten
    => [{:id=>1, :name=>"Immad", :age=>18}, {:id=>2, :name=>"Vicky", :age=>21}, {:id=>3, :name=>"Adam", :age=>24}]
Mayur Shah
  • 3,344
  • 1
  • 22
  • 41
0

Use .map!:

foo = [
  [
    {
        id: 1,
        name: "Immad",
        age: 18
    }
  ],
  [
    {
        id: 2,
        name: "Vicky",
        age: 21
    }
  ],
  [
    {
        id: 3,
        name: "Adam",
        age: 24
    }
  ]
]

# Call .map! on the array
foo.map! { |array| array.first }
=> [
    [0] {
          :id => 1,
        :name => "Immad",
         :age => 18
    },
    [1] {
          :id => 2,
        :name => "Vicky",
         :age => 21
    },
    [2] {
          :id => 3,
        :name => "Adam",
         :age => 24
    }
]
anothermh
  • 9,815
  • 3
  • 33
  • 52