Reputation: 3297
I have a big array like:
[
{id: 1, name: 'abs', surname: 'abs'},
{id: 1, name: 'abs', surname: 'abs'},
...
{id: 1, name: 'abs', surname: 'abs'},
]
and I need to add new 'group' key into each hash. First five hashes should have 'group': 1, second five hashes should have 'group': 2 e.g. to get in result array:
[
{id: 1, name: 'abs', surname: 'abs', group: 1},
{id: 1, name: 'abs', surname: 'abs', group: 1},
{id: 1, name: 'abs', surname: 'abs', group: 1},
{id: 1, name: 'abs', surname: 'abs', group: 1},
{id: 1, name: 'abs', surname: 'abs', group: 1},
{id: 1, name: 'abs', surname: 'abs', group: 2},
...
{id: 1, name: 'abs', surname: 'abs', group: N},
]
If I use Array#group_by and Array#each_with_index methods code works slow, cause array have many records... How can I do it using ruby to make code run faster?
Upvotes: 0
Views: 63
Reputation: 3297
how about:
foo = [
{id: 1, name: 'abs', surname: 'abs'},
{id: 1, name: 'abs', surname: 'abs'},
{id: 1, name: 'abs', surname: 'abs'},
{id: 1, name: 'abs', surname: 'abs'},
]
group_count =5
foo.each_with_index do |u,i|
u['highlight_group'] = i.div( group_count )
end
puts foo
Upvotes: 0
Reputation: 160551
I reduced your input sample because there was no need for that large of a sample. You can interpolate how to do what you need from this code:
require 'json'
foo = [
{id: 1, name: 'abs', surname: 'abs'},
{id: 1, name: 'abs', surname: 'abs'},
{id: 1, name: 'abs', surname: 'abs'},
{id: 1, name: 'abs', surname: 'abs'},
]
foo.each_slice(2).with_index(1){ |a, i| a.each{ |h| h[:group] = i } }
puts foo.to_json
# >> [{"id":1,"name":"abs","surname":"abs","group":1},{"id":1,"name":"abs","surname":"abs","group":1},{"id":1,"name":"abs","surname":"abs","group":2},{"id":1,"name":"abs","surname":"abs","group":2}]
Here's what it looks like broken out to be more readable:
puts JSON::pretty_generate(foo)
# >> [
# >> {
# >> "id": 1,
# >> "name": "abs",
# >> "surname": "abs",
# >> "group": 1
# >> },
# >> {
# >> "id": 1,
# >> "name": "abs",
# >> "surname": "abs",
# >> "group": 1
# >> },
# >> {
# >> "id": 1,
# >> "name": "abs",
# >> "surname": "abs",
# >> "group": 2
# >> },
# >> {
# >> "id": 1,
# >> "name": "abs",
# >> "surname": "abs",
# >> "group": 2
# >> }
# >> ]
Here's what is happening:
each_slice
slices up the array into N-sized chunks.
pp foo.each_slice(2).to_a
# >> [[{:id=>1, :name=>"abs", :surname=>"abs", :group=>1},
# >> {:id=>1, :name=>"abs", :surname=>"abs", :group=>1}],
# >> [{:id=>1, :name=>"abs", :surname=>"abs", :group=>2},
# >> {:id=>1, :name=>"abs", :surname=>"abs", :group=>2}]]
with_index
adds an incrementing value to the list of parameters passed into the block. That is being assigned to i
in the block.
pp foo.each_slice(2).with_index(1).to_a
# >> [[[{:id=>1, :name=>"abs", :surname=>"abs", :group=>1},
# >> {:id=>1, :name=>"abs", :surname=>"abs", :group=>1}],
# >> 1],
# >> [[{:id=>1, :name=>"abs", :surname=>"abs", :group=>2},
# >> {:id=>1, :name=>"abs", :surname=>"abs", :group=>2}],
# >> 2]]
Upvotes: 1