1

Lets say I have this data :

[{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"},
[{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, 
[], nil,{"sku"=>"poster"}, 
[], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"},
[{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, 
[], nil, {"quantity"=>"5"},
[], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil] 

I want to iterate this data and print it in html in a proper format using Ruby on Rails I'm new to this technology. Any help is appreciated!

4
  • Use an each loop to iterate through the data and output it... If you want more information on a step, show us what you have and provide more details on your application. Commented Dec 13, 2012 at 13:28
  • 2
    Maybe you could let us know what output you would expect for this data Commented Dec 13, 2012 at 13:31
  • 4
    What is proper format for you? Commented Dec 13, 2012 at 13:32
  • Thanks all for the reply..But im not aware of iterating the inner details of this..I want each to them to be shown in a separate line with key value pair..Kindly help me in iterating this..As I have both Array and Hash inside the outer array,..how do i iterate both of them together? Commented Dec 13, 2012 at 15:03

1 Answer 1

2

.flatten might be handy here. If you're not fussed about the structure, I'd just flatten it before iterating then you don't have to worry about varying depth.

1.9.3p194 :001 > root = [{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"},
1.9.3p194 :002 >     [{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, 
1.9.3p194 :003 >     [], nil,{"sku"=>"poster"}, 
1.9.3p194 :004 >     [], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"},
1.9.3p194 :005 >     [{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, 
1.9.3p194 :006 >     [], nil, {"quantity"=>"5"},
1.9.3p194 :007 >     [], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil]
 => [{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"}, [{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, [], nil, {"sku"=>"poster"}, [], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"}, [{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, [], nil, {"quantity"=>"5"}, [], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil] 
1.9.3p194 :008 > root.flatten!
 => [{"sku"=>"reprint"}, nil, {"quantity"=>"1"}, nil, {"shippingOptions"=>"PQRS"}, {"option"=>"PQRS"}, nil, {"abc"=>"abcccc"}, nil, {"sku"=>"poster"}, nil, {"quantity"=>"2"}, nil, {"shippingOptions"=>"MNLIJK"}, {"option"=>"MNL"}, {"option"=>"IJK"}, nil, {"sku"=>"cards"}, nil, {"quantity"=>"5"}, nil, {"shippingOptions"=>"DEFABC"}, {"option"=>"DEF"}, {"option"=>"ABC"}, nil]  
1.9.3p194 :011 > root.each do |r| 
1.9.3p194 :012 >     next if r.nil?
1.9.3p194 :013?>     puts r.to_s
1.9.3p194 :014?> end
{"sku"=>"reprint"}
{"quantity"=>"1"}
{"shippingOptions"=>"PQRS"}
{"option"=>"PQRS"}
{"abc"=>"abcccc"}
{"sku"=>"poster"}
{"quantity"=>"2"}
{"shippingOptions"=>"MNLIJK"}
{"option"=>"MNL"}
{"option"=>"IJK"}
{"sku"=>"cards"}
{"quantity"=>"5"}
{"shippingOptions"=>"DEFABC"}
{"option"=>"DEF"}
{"option"=>"ABC"}

RE: comments.

data = [{"sku"=>"reprint"}, [], nil, {"quantity"=>"1"}, [], nil, {"shippingOptions"=>"PQRS"}, [{"option"=>"PQRS"}], nil, {"abc"=>"abcccc"}, [], nil, {"sku"=>"poster"}, [], nil, {"quantity"=>"2"}, [], nil, {"shippingOptions"=>"MNLIJK"}, [{"option"=>"MNL"}, {"option"=>"IJK"}], nil, {"sku"=>"cards"}, [], nil, {"quantity"=>"5"}, [], nil, {"shippingOptions"=>"DEFABC"}, [{"option"=>"DEF"}, {"option"=>"ABC"}], nil]
hash_collection = data.flatten.map(&:to_a).flatten(1).reduce({}) { |h,(k,v)| (h[k] ||= []) << v; h}

produces:

=> {"sku"=>["reprint", "poster", "cards"], "quantity"=>["1", "2", "5"], "shippingOptions"=>["PQRS", "MNLIJK", "DEFABC"], "option"=>["PQRS", "MNL", "IJK", "DEF", "ABC"], "abc"=>["abcccc"]}

I actually want my new data to be some what like this :

{{"sku"=>"Reprint"},{"quantity"=>"1"},{"option"=>"PQRS"}}
{{"sku"=>"Poster"},{"quantity"=>"2"},{"option"=>"MNL"},{"option"=>"IJK"}}
{{"sku"=>"Cards"}, {"quantity"=>"5"},{"option"=>"DEF"},{"option"=>"ABC"}}
{{"sku"=>"Books"},{"quantity"=>"6"},{"option"=>"QIU"},{"option"=>"PSJ"},   option"=>"IQA"},{"suboption"=>"ikh"},{"subsuboption"=>"uuuuuuuuuuuuuuuuu"}}
Sign up to request clarification or add additional context in comments.

3 Comments

If I want to group particular things and print then how would I be able to do it? For example, I want to group sku,quantity,option together..Any help is appreciated
On your flattened array: data.map(&:to_a).flatten(1).reduce({}) { |h,(k,v)| (h[k] ||= []) << v; h}. Credit to this fella.
Im now getting the data like this {"option"=>[nil], "PQRS"=>[nil]} {"sku"=>[nil], "Reprint"=>[nil]} {"quantity"=>[nil], "1"=>[nil]} {"option"=>[nil], "MNL"=>[nil]} {"option"=>[nil], "IJK"=>[nil]} {"sku"=>[nil], "Poster"=>[nil]} {"quantity"=>[nil], "2"=>[nil]} {"option"=>[nil], "DEF"=>[nil]} {"option"=>[nil], "ABC"=>[nil]}

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.