2015-07-20 104 views
0
的陣列

我的散列的數組類似於刪除零值:從哈希

[ 
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, 
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, 
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, 
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}, 
    {:id=>nil, :name=>nil, :type=>nil}, 
    {:id=>nil, :name=>nil, :type=>nil}, 
    {:id=>nil, :name=>nil, :type=>nil}, 
    {:id=>nil, :name=>nil, :type=>nil}, 
    {:id=>nil, :name=>nil, :type=>nil}, 
    {:id=>nil, :name=>nil, :type=>nil}, 
    {:id=>nil, :name=>nil, :type=>nil} 
] 

如何刪除nil值,如果在我的哈希idnil

+3

請編輯澄清(即使您選擇了答案,因爲很多人將來可能會閱讀您的問題)。 「如果我的散列'id'爲'nil',我怎麼能刪除'nil'值?沒有任何意義,你可以在不刪除鍵的情況下刪除值,也許你的意思是你想刪除所有散列'h',其中'h [:id] .nil?#=> true'?如果是這樣的話(如果你的數組是'a',你想修改'a'還是返回一個等於'a'的數組,元素被刪除?),準確地說Ruby是所有的東西! –

回答

4

您可以使用reject

arr.reject{|h| h[:id].nil?} 

=> [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, 
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, 
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, 
    {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}] 

Demo

或者select

arr.select { |h| h[:id] } 

Demo

0

我假設你想獲得其值不都所有哈希。你可以這樣做:

arr = [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}, {:id=>nil, :name=>nil, :type=>nil}] 

arr.find_all { |h| !h.values.compact.empty? } 
# => [{:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"}] 
0

你想使用select method過濾數組。

list = list.select { |item| item[:id] != nil } 
1

如果您關注的是除去元素,其哈希idnil則最好使用delete_if

array.delete_if { |h| h[:id].nil? } 
=> [ 
     {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!107", :name=>"Folder 11", :type=>"folder"}, 
     {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!105", :name=>"Pictures", :type=>"album"}, 
     {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!113", :name=>"Public", :type=>"folder"}, 
     {:id=>"folder.cecb8eb83d90dbeb.CECB8EB83D90DBEB!124", :name=>"sasd", :type=>"folder"} 
    ] 

注:reject會返回一個新陣列包含的項目在self中,給定的塊不是true。而delete_if將刪除self的每個元素,其塊評估爲true