Ruby找到并返回对象数组中基于属性 - Ruby find and return objects in an array based on an attribute

- 此内容更新于:2016-02-02
主题:

你怎么能遍历一个对象数组并返回整个对象如果某个属性是正确的?我在rails应用程序有以下但出于某种原因,这两个返回相同的结果!我试图取代,而不是和他们工作!任何帮助都是高度赞赏!

原文:

How can you iterate through an array of objects and return the entire object if a certain attribute is correct?

I have the following in my rails app

array_of_objects.each { |favor| favor.completed == false }

array_of_objects.each { |favor| favor.completed }

but for some reason these two return the same result! I have tried to replace each with collect, map, keep_if as well as !favor.completed instead of favor.completed == false and none of them worked!

Any help is highly appreciated!

楼主:我想要的是如果,如果返回整个对象

(原文:What I want is to return the entire object favor if favor.completed and if !favor.completed)

网友:该方法总是返回原来的数组。

(原文:the each method always returns the original array.)

网友:@sugar资格的评论,当它有一个块,数组#每个返回其接收机。

(原文:Just to qualify @sugar's comment, when it has a block, Array#each returns its receiver.)

网友:你是什么意思,“返回整个对象”?你的意思是你想返回一个数组的对象从第一个数组的一个给定的属性评估?或评估“假”?

(原文:What do you mean by, "return the entire object"? Do you mean you want to return an array of those objects from the first array for which a given attribute evaluates true? Or evaluates 'false'?)

网友:是的这就是我的意思。我不想返回的属性

(原文:yes that's what I meant. I don't want to have the attribute returned)

解决方案:
第一个案例,案例二,
原文:

For first case,

array_of_objects.reject(&:completed)

For second case,

array_of_objects.select(&:completed)
网友:好,做的,假设我们都理解正确的问题。顺便说一句,我看到了魔杖没有更多。

(原文:Nicely, done, assuming we both understand the question correctly. btw, I see the wand is no more.)

网友:@CarySwoveland感谢的话。

(原文:@CarySwoveland Thanks for the kind words.)

解决方案:
您需要使用所有匹配的对象。
原文:

You need to use Enumerable#find_all to get the all matched objects.

array_of_objects.find_all { |favor| favor.completed == false }
网友:但我需要所有匹配的对象,而不是第一个!

(原文:But I need all matched objects instead of the first one!)

网友:@fardin误读了..:)

(原文:@fardin misread.. :))

网友:和“find_all”都给我返回nil!(非常烦人

(原文:both find and 'find_all'` are returning nil for me! :( very annoying)

网友:这是我打电话的对象数组

(原文:this is the array of objects I'm calling them on [#<RequestedFavor id: 8, title: "Moving day", description: "Help me move to my new flat please!", user_id: 1, completed: false, created_at: "2016-01-30 18:19:33", updated_at: "2016-01-30 18:19:33", type: "RequestedFavor">, #<OfferedFavor id: 9, title: "Got two hours free", description: "I can spare two hours on Friday", user_id: 1, completed: false, created_at: "2016-01-30 18:19:52", updated_at: "2016-01-30 18:19:52", type: "OfferedFavor">])

解决方案:
将返回所有完成的对象是错误的。您还可以使用代替。
原文:
array_of_objects.select { |favor| favor.completed == false }

Will return all the objects that's completed is false.

You can also use find_all instead of select.

网友:有非破坏性方法相似吗?

(原文:Are there any non-destructive methods similar to select?)

网友:不明白,对不起。

(原文:Didn't understand non-destructive, sorry.)

网友:改变原来的数组,我在想如果有一个类似的专业方法,使得原始数组完好无损?

(原文:select changes the original array, I was just wondering if there's a similar specialised method that keeps the original array intact?)

网友:没有不会改变。所做的事。

(原文:No select doesn't change self. select! does.)

网友:哦,是的,我看到了。有困惑了。

(原文:Oh yes, I see now. Got confused now.)