简体   繁体   English

rails-从数组中检索某些值

[英]rails - retrieve certain values from array

I am calling an api to get a response. 我正在调用一个API以获得响应。

users = response.involved_users // here users is an array with everything, I only want the actual user numbers

Part of this response has an array. 此响应的一部分包含一个数组。

The array can have one or more elements. 该数组可以具有一个或多个元素。

In each element, there are values of 在每个元素中,都有以下值

@type = "start"
@value = "this value"
@user = "12345"

I don't know if these are stored as a hash in the elements or what. 我不知道这些是否存储为元素中的哈希或什么。

I want to pull all the @user - user numbers in to an array. 我想将所有@user用户号拉到一个数组中。

So for every elemnent, add the user to an array. 因此,对于每个元素,将用户添加到数组中。

so this line, user should be an array of users: 所以这一行,user应该是一个用户数组:

users = response.involved_users // do I need to do a map?

I tried this: 我尝试了这个:

users = response.involved_users.map { |x| x[:user]

but got this: 但是得到这个:

NoMethodError - undefined method `[]' for #<Client::InvolvedUser:0x11691828>:
    #<Client::InvolvedUser:0x11691828>:

shows that x is an object of Class Client::InvolvedUser so an object's attribute should be called like this 显示x是Class Client :: InvolvedUser的对象,因此应像这样调用对象的属性

    x.user

Try this 尝试这个

    users = response.involved_users.map { |x| x.user }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM