rails——从数组中检索某些值

时间:2021-09-19 13:27:30

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 - user - user号拉到一个数组中。

So for every elemnent, add the user to an array.

对于每个元素,将用户添加到数组中。

so this line, user should be an array of users:

所以这条线,用户应该是一个用户群:

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>:

1 个解决方案

#1


0  

    #<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:: veduser的对象,因此应该像这样调用对象的属性

    x.user

Try this

试试这个

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

#1


0  

    #<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:: veduser的对象,因此应该像这样调用对象的属性

    x.user

Try this

试试这个

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