Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting Array of Array of Hashes to Array of Hashes
    primarykey
    data
    text
    <p>I have an array of arrays of hashes.</p> <pre><code>items = [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 144 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 146 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 147 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 148 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 153 }] . . . [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 144 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 146 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 147 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 148 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 153 }] </code></pre> <p>What I'd like to do is change it to be an array of hashes...</p> <pre><code>items = {"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;146} {"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;147} {"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;148} {"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;153} </code></pre> <p>I believe I can do this using...</p> <pre><code>items.map! { |item| item.reduce({}, :merge) } </code></pre> <p>However, it's not very performant. At least it's not performant enough when you have 140million records. Is there a better way to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload