ruby on rails - Check and select the latest one from the array of hashes -
i have check , select latest 1 array of hashes. structure this:
'histories':[ { { ... }, 'created': "date1", 'items':[ { 'a': "ready", 'b': "dfknsknfs", }, { 'a': "sdfjbsf", 'b': "hello23", } ] }, { { ... }, 'created': "date2", 'items':[ { 'a': "sknfkssd", 'b': "ksdfjshs", }, { 'a': "ready", 'b': "shdfjsh", } ] }, ... ]
i have first find value "ready" , have select latest "created" date.
my attempt like
ready_item = histories.select { |item| item.items.detect {|f| f.a == "ready" } } ready_item
but since detect
used, returning first detected value. need latest date. should possible solution of it?
histories.select { |h| h[:items].detect {|f| f[:a] == 'ready' } }.sort_by {|x| x[:created_at] }.last
Comments
Post a Comment