Assalamualaikum
Katakan kita ada satu array
Bagaimana pula untuk kita elakkan dari tarikh yang sama berulang?
Katakan kita ada satu array
students [ [0] { :id => 1, :staff_no => "A2123", :name => "Ahmad Aaron", :phone => "2932-233-3333", :registered_at => "2019-12-11 12:22:22" }, [1] { :id => 2, :staff_no => "A2123", :name => "Bustaman Baharun", :phone => "1223-231-2222", :registered_at => "2019-11-21 12:22:22" }, [2] { :id => 3, :staff_no => "A3123", :name => "Charlie Chaplin", :phone => "4554-344-5554", :registered_at => "2018-02-16 12:22:22" }, [3] { :id => 4, :staff_no => "A2523", :name => "Daruma Dorayaki", :phone => "5667-665-6655", :registered_at => "2017-06-14 12:22:22" }, [4] { :id => 5, :staff_no => "A4523", :name => "Edward Elrick", :phone => "8778-223-9988", :registered_at => "2017-01-22 12:22:22" }, [5] { :id => 6, :staff_no => "A4623", :name => "Frank Frederick", :phone => "2443-544-3445", :registered_at => "2017-01-22 11:01:23" }, [6] { :id => 7, :staff_no => "A2567", :name => "Goro Gokurojo", :phone => "5667-665-6255", :registered_at => "2017-06-14 14:12:11" } ]Bagaimana cara untuk kita susun (sort) berdasarkan tarikh dan bukan tarikh masa menggunakan Ruby? kita guna guna sort_by
students.sort_by{|s|s[:registered_at]} [ [0] { :id => 6, :staff_no => "A4623", :name => "Frank Frederick", :phone => "2443-544-3445", :registered_at => "2017-01-22 11:01:23" }, [1] { :id => 5, :staff_no => "A4523", :name => "Edward Elrick", :phone => "8778-223-9988", :registered_at => "2017-01-22 12:22:22" }, [2] { :id => 4, :staff_no => "A2523", :name => "Daruma Dorayaki", :phone => "5667-665-6655", :registered_at => "2017-06-14 12:22:22" }, [3] { :id => 7, :staff_no => "A2567", :name => "Goro Gokurojo", :phone => "5667-665-6255", :registered_at => "2017-06-14 14:12:11" }, [4] { :id => 3, :staff_no => "A3123", :name => "Charlie Chaplin", :phone => "4554-344-5554", :registered_at => "2018-02-16 12:22:22" }, [5] { :id => 2, :staff_no => "A2123", :name => "Bustaman Baharun", :phone => "1223-231-2222", :registered_at => "2019-11-21 12:22:22" }, [6] { :id => 1, :staff_no => "A2123", :name => "Ahmad Aaron", :phone => "2932-233-3333", :registered_at => "2019-12-11 12:22:22" } ]
Bagaimana pula untuk kita elakkan dari tarikh yang sama berulang?
kita gabungkan dengan .uniq
students.sort_by{|s|s[:registered_at]}.uniq{|q| q[:registered_at].to_date} [ [0] { :id => 6, :staff_no => "A4623", :name => "Frank Frederick", :phone => "2443-544-3445", :registered_at => "2017-01-22 11:01:23" }, [1] { :id => 4, :staff_no => "A2523", :name => "Daruma Dorayaki", :phone => "5667-665-6655", :registered_at => "2017-06-14 12:22:22" }, [2] { :id => 3, :staff_no => "A3123", :name => "Charlie Chaplin", :phone => "4554-344-5554", :registered_at => "2018-02-16 12:22:22" }, [3] { :id => 2, :staff_no => "A2123", :name => "Bustaman Baharun", :phone => "1223-231-2222", :registered_at => "2019-11-21 12:22:22" }, [4] { :id => 1, :staff_no => "A2123", :name => "Ahmad Aaron", :phone => "2932-233-3333", :registered_at => "2019-12-11 12:22:22" } ]
jadi.. untuk hanya memaparkan tarikh pendaftaran dalam satu array yang panjang, kita gabungkan sekali lagi dengan map
students.sort_by{|s|s[:registered_at]}.uniq{|q| q[:registered_at].to_date}.map{|c| c[:registered_at].to_date} [ [0] Sun, 22 Jan 2017, [1] Wed, 14 Jun 2017, [2] Fri, 16 Feb 2018, [3] Thu, 21 Nov 2019, [4] Wed, 11 Dec 2019 ]
Ulasan
Catat Ulasan
Comment