Home > Mobile >  How to create an array from an object and only keep positive values?
How to create an array from an object and only keep positive values?

Time:12-08

I am building a rails 5.2 app.

In this app I need to convert an object to an array, but only keep the attributes that got a true value (in this example notification_email)

The object is this:

{"notification_email"=>true, "notification_alert"=>false}

I want to achieve this:

["notification_email"]

CodePudding user response:

x = {"notification_email"=>true, "notification_alert"=>false}

result = x.delete_if {|x,y| y == false}.keys

["notification_email"]

  • Related