我正在研究 Rails 3.0。我有一个二维数组。二维数组由用户数据和布尔值组成。
例如: [ [user1,true], [user2,true], [user3,false] ]
它看起来像这样:
[
[#<User id: 1, email: "[email protected]", username: "abc">, true],
[#<User id: 2, email: "[email protected]", username: "ijk">, true],
[#<User id: 3, email: "[email protected]", username: "xyz">, false],
]
我想有条件地查找/提取记录;假设查找 User id=2
的整行,它应该只返回第二行,即 [#[电子邮件受保护]", username: "ijk">, true]
是否有办法循环访问这样的数组?如何才能实现呢?
I am working on Rails 3.0. I have a two dimensional array. The two dimensional array consists of user data and a boolean value.
For example: [ [user1,true], [user2,true], [user3,false] ]
It looks something like this:
[
[#<User id: 1, email: "[email protected]", username: "abc">, true],
[#<User id: 2, email: "[email protected]", username: "ijk">, true],
[#<User id: 3, email: "[email protected]", username: "xyz">, false],
]
I want to find/extract records conditionally; say finding an entire row where User id=2
, it should return only the second row i.e. [#<User id: 2, email: "[email protected]", username: "ijk">, true]
Is there anyway to loop through such arrays? How can it be achieved?
发布评论
评论(3)
所有带有 true flag:
或 false: 的用户
all users with true flag:
or false:
您可以执行类似的操作
,这将仅返回用户 id 等于 2 的记录。
You can do something like
This will return only the records that have the user id equal to 2.
与@eugen相同的答案,仅语法差异(并使用检测返回单维数组而不是二维数组):
Same answer as @eugen, only syntax difference(and using detect to return single dimensional array instead of 2 dimensional array):