0

Sorry to ask, but I'm struggling somewhat. Essentially I have the following function:

weekly_reach_data = weekly_reach_arr.map do |data| [
    {
       name: "DCable",
       data: {x: data['Graph numerics'].to_i, y: data['DCable'].to_f}
    },
    {
        name: "DSat",
        data: {x: data['Graph numerics'].to_i, y: data['DSat'].to_f}
    },
    {
        name: "DTT",
        data: {x: data['Graph numerics'].to_i, y: data['DTT'].to_f}
    }
]

end

This outputs data in this form:

{:name=>"DCable", :data=>{:x=>1, :y=>1.8}}
{:name=>"DSat", :data=>{:x=>1, :y=>7.48}}
{:name=>"DTT", :data=>{:x=>1, :y=>5.81}}
{:name=>"DCable", :data=>{:x=>2, :y=>2.29}}
{:name=>"DSat", :data=>{:x=>2, :y=>7.74}}
{:name=>"DTT", :data=>{:x=>2, :y=>5.82}} etc

However, I require the data to be in this form:

{:name=>"DCable", :data=>[{:x=>1, :y=>2}, {:x=>2, :y=>3}]}
{:name=>"DSat", :data=>[{:x=>1, :y=>5}, {:x=>2, :y=>8}]}
{:name=>"DTT", :data=>[{:x=>1, :y=>3}, {:x=>2, :y=>9}]}

As you can see, the former is looping and creating multiple entries. Does anybody know how I can reconstruct the map function to output in this form?

Weekly_reach_arr is an array in this form:

{"Graph numerics"=>"1", "Users"=>"S", "All platforms"=>"14.99", "DSat"=>"7.48", "DTT"=>"5.81", "DCable"=>"1.80", "% Reach"=>"S", "Target"=>"33.33%"}
{"Graph numerics"=>"2", "Users"=>"O", "All platforms"=>"15.60", "DSat"=>"7.74", "DTT"=>"5.82", "DCable"=>"2.29", "% Reach"=>"O", "Target"=>"33.33%"}
{"Graph numerics"=>"3", "Users"=>"N", "All platforms"=>"16.83", "DSat"=>"7.78", "DTT"=>"6.65", "DCable"=>"2.52", "% Reach"=>"N", "Target"=>"33.33%"}
{"Graph numerics"=>"4", "Users"=>"D", "All platforms"=>"16.27", "DSat"=>"7.64", "DTT"=>"7.01", "DCable"=>"1.68", "% Reach"=>"D", "Target"=>"33.33%"}... etc
3
  • 1
    what does weekly_reach_arr look like? Commented Dec 22, 2014 at 11:21
  • Does it need to be output exactly like that? You could lose the name and data keys and flatten it to a Hash that has the name as the key and the data as the value Commented Dec 22, 2014 at 11:27
  • Unfortunately yes. I'm pushing this to a Dashing Rickshaw Graph. Rickshaw only accepts arrays in a particular style. Commented Dec 22, 2014 at 11:32

2 Answers 2

3

Made a few edits but I think the following is the most succinct version:

%w{DCable DSat DTT}.map do |name|
  weekly_reach_arr.each_with_object({name: name, data: []}) do |data, hash|
    hash[:data] << {x: data['Graph numerics'].to_i, y: data[name].to_f}
  end
end
Sign up to request clarification or add additional context in comments.

Comments

0

You could lose flatten the resulting dictionary slightly and use

result = Hash.new { |h,k| h[k] = [] }

weekly_reach_arr.each do |data|
  %w(DSat DTT DCable).each do |key|
    result[key] << { x: data["Graph numerics"].to_i, y: data[key].to_f }  
  end
end

which would give

{"DSat"=>
  [{:x=>1, :y=>7.48},
   {:x=>2, :y=>7.74},
   {:x=>3, :y=>7.78},
   {:x=>4, :y=>7.64}],
 "DTT"=>
  [{:x=>1, :y=>5.81},
   {:x=>2, :y=>5.82},
   {:x=>3, :y=>6.65},
   {:x=>4, :y=>7.01}],
 "DCable"=>
  [{:x=>1, :y=>1.80},
   {:x=>2, :y=>2.29},
   {:x=>3, :y=>2.52},
   {:x=>4, :y=>1.68}]}

You could always map this back to how you need it with

result.map { |key, value|
  { name: key, data: value }
}

which gives

{:name=>"DSat", :data=>[{:x=>1, :y=>7.48}, {:x=>2, :y=>7.74}, {:x=>3, :y=>7.78}, {:x=>4, :y=>7.64}]}
{:name=>"DTT", :data=>[{:x=>1, :y=>5.81}, {:x=>2, :y=>5.82}, {:x=>3, :y=>6.65}, {:x=>4, :y=>7.01}]}
{:name=>"DCable", :data=>[{:x=>1, :y=>1.8}, {:x=>2, :y=>2.29}, {:x=>3, :y=>2.52}, {:x=>4, :y=>1.68}]}

1 Comment

@Humza 's answer is more succinct - I'd recommend taking that one

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.