0
votes

I have an array of array called 'data' which is: enter image description here (built it to adjust to use sb's advice on Active Admin)

I need to use its data to create a array of hash with this structure:

data_prepared_for_table = [{goal: data[0][0], number: data[0][1]},
                    {goal: data[1][0], number: data[1][1]},
                    {goal: data[2][0], number: data[2][1]},
                    {goal: data[3][0], number: data[3][1]},
                    {goal: data[4][0], number: data[4][1]},
                    {goal: data[5][0], number: data[5][1]}
                   ]

in data_prepared_for_table, Goal has to display the first part of the array 'date': acquisition, branding, qualification.

Number has to display the related quantities in the array 'data' such as 3 , 2, 1.

I'd like to do 2 things:

  • streamline/improve the code by using a lop each or for, that would enable me to generate data_prepared_for_table something like:

     data_prepared_for_table = [(0..5).each do |i|
                        {goal: data[i][0], number: data[i][1]},                        
                       ]
    

    But it doesn't work and creates an error. How should I do it?

  • another thing is that in the example I give it goes up to data [5][0] but I want to be able to not specify the end (5 here) in case the array get longer and goes up to data [6] or even more [7] , and so on... I actually don't know in advance how long it will get!

How can I do it without specifying the last value of i? Maybe in the iteration, specify that i has to increment until data[i][0].exists??

1
can you show an expected output what does goal and data refer to? - bjhaid
@bjhaid just added some explanation. It's very basic: i have an array with goal_type and for each goal_type a number. I need to use the information that is inside the array called 'data' to create another array called data_prepared_for_table. i manage to do it but as i said i'd like to DRY/streamline/reduce the code thanks to a loop/iteration if possible. I also need to provide for the case where i don't know what will be the number of values in the array. - Mathieu

1 Answers

1
votes

You just need to throw a little map in the mix to map your array to the format your want:

array_of_hashes = array_of_arrays.map { |a| { :goal => a.first, :number => a.last.to_i } }

You may or may not want the to_i call but it probably makes sense given the history of your task.

You could also combine this with where the original query:

array_of_hashes = Deal.connection.select_rows(...).map do |row|
  { :goal => row.first, :number => row.last.to_i }
end

to avoid the intermediate array (assuming of course that you don't need the array-of-arrays for anything).


The problem with this:

data_prepared_for_table = [(0..5).each do |i|
                {goal: data[i][0], number: data[i][2]},                        
               ]

is that you have a stray comma inside the each block, you're looking at data[i][2] instead of data[i][1], and most importantly, each returns its receiver so you're really writing this:

data_prepared_for_table = [ (0..5) ]

with a bunch of extra noise.