ruby - 如果数组中的对象包含 Ruby 中的所需值,如何获取索引

标签 ruby

所以这是我从某处获得的数组,我想获得包含“文本”=>“ Assets ”的对象的索引。我如何在 Ruby 中做到这一点?

例子

[{
                     "class" => "android.support.v7.widget.AppCompatTextView",
                       "tag" => nil,
               "description" => "android.support.v7.widget.AppCompatTextView{b777d8c V.ED..... ........ 0,39-355,168 #7f0e007f app:id/textview_sectiontitle}",
                        "id" => "textview_sectiontitle",
                      "text" => "Assets",
                   "visible" => true,
                      "rect" => {
              "height" => 129,
               "width" => 355,
                   "y" => 2088,
                   "x" => 80,
            "center_x" => 257,
            "center_y" => 2152
        },
                   "enabled" => true,
        "contentDescription" => nil
    },
    {
                     "class" => "android.support.v7.widget.AppCompatImageButton",
                       "tag" => nil,
               "description" => "android.support.v7.widget.AppCompatImageButton{ae57704 VFED..C.. ........ 1056,0-1280,208 #7f0e0109 app:id/imagebutton_amount}",
                        "id" => "imagebutton_amount",
                   "visible" => true,
                      "rect" => {
              "height" => 208,
               "width" => 224,
                   "y" => 2049,
                   "x" => 1136,
            "center_x" => 1248,
            "center_y" => 2153
        },
                   "enabled" => true,
        "contentDescription" => nil
    }]

最佳答案

试试这个

arr.index { |item| item["text"] == "Assets" }

关于ruby - 如果数组中的对象包含 Ruby 中的所需值,如何获取索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44694217/

相关文章:

ruby - Gem 安装错误 : You have to install development tools first (Windows)

ruby - rpsec的 "let"是如何使用memoization的?

ruby-on-rails - Rails View 中的三元运算符

ruby-on-rails - (Rails) Nokogiri 从 URL 返回 nil

ruby - method_missing in "Programming Ruby"在我头上

ruby - 为什么我可以在不需要 'date' 的情况下使用 Date 类?

ruby - 在 Ruby Rails 安装上找不到 rake

ruby - 在 ruby​​ 数组中查找整数 (Fixnum) 值

ruby-on-rails - Rails 如何创建数据模式种子数据

ruby - OS X lion rvm、ruby 安装成功,但不执行。我错过了什么?