How do I remove blank elements from an array?

前端 未结 20 711
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 16:43

I have the following array

cities = [\"Kathmandu\", \"Pokhara\", \"\", \"Dharan\", \"Butwal\"]

I want to remove blank elements from the ar

20条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 17:20

    Here is one more approach to achieve this

    we can use presence with select

    cities = ["Kathmandu", "Pokhara", "", "Dharan", nil, "Butwal"]
    
    cities.select(&:presence)
    
    ["Kathmandu", "Pokhara", "Dharan", "Butwal"]
    

提交回复
热议问题