简体   繁体   English

在Rails中,如何确定对象数组是否包含与给定值匹配的特定属性?

[英]In Rails, how do I figure out if an array of objects contains specific attributes matching given values?

I'm using Ruby on Rails 5.0.1 with Ruby 2.4. 我在Ruby 2.4上使用Ruby on Rails 5.0.1。 I have an array of objects, stored in the array, "results." 我有一个对象数组,存储在数组“结果”中。 Each object has a numeric attribute 每个对象都有一个数字属性

numeric_attr

I would like to know, given my array, how I can tell if I have exactly one object with a numeric attribute value of "1" and incrementing by one. 我想知道,在给定数组的情况下,如何确定我是否只有一个对象的数值属性值为“ 1”并递增1。 Order is not important. 顺序并不重要。 So, for instance, if I have an array of three objects, 因此,例如,如果我有一个由三个对象组成的数组,

[MyObject(numeric_attr = 2), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]

I want to know if I have exactly one object with numeric_attr = 1, another object with numeric_attr = 2, and another with numeric_attr = 3. So the above satisfies the condition. 我想知道是否有一个对象的numeric_attr = 1,另一个对象的numeric_attr = 2,另一个对象的numeric_attr =3。因此,上述条件可以满足。 The below example does not 下面的例子不

[MyObject(numeric_attr = 4), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]

because although there is an object with numeric_attr = 1, there is no object with numeric_attr = 2. It is possible thet the numeric_attr field is nil. 因为尽管有一个对象的numeric_attr = 1,但是没有对象的numeric_attr =2。有可能thenumeric_attr字段为nil。 How can I figure this out? 我该如何解决?

This one-liner should work: 这种单线工作:

results.map(&:numeric_attr).sort == (1..results.count).to_a

Explanation: 说明:

results
#=> [#<MyObject:... @attr=2>, #<MyObject:... @attr=3>, #<MyObject:... @attr=1>]

results.map(&:attr)
#=> [2, 3, 1]

results.map(&:attr).sort
#=> [1, 2, 3]

(1..results.length).to_a
#=> [1, 2, 3]

# therefore:
results.map(&:attr).sort == (1..results.count).to_a
#=> true

If there is a chance that numeric_attr is nil : 如果numeric_attr有可能为nil

results.map(&:attr).compact.sort == (1..results.count).to_a

Of course, if there is even a single nil value, the result is guaranteed to be false . 当然,即使只有一个nil值,也保证结果为false

If the sequence could start at any number, not just 1: 如果序列可以以任意数字开头,而不仅仅是1:

results.map(&:attr).sort == results.count.times.to_a.
  map { |i| i + results.map(&:attr).sort.first }

This is not very efficient though, as it sorts the numbers twice. 但是,这并不是很有效,因为它会将数字排序两次。

If they always start at 1 @Máté's solution works, if they can start at any arbitrary number then you could: 如果它们始终从1开始,@Máté的解决方案有效,如果它们可以从任意数字开始,则可以:

count = 0
array_objects.sort_by(&:numeric_attr).each_cons(2) {|a,b| count+=1 if a.numeric_attr==b.numeric_attr-1 }
count+1==array_objects.count

Not as elegant but handles a lot more situations 不那么优雅,但可以处理更多情况

暂无
暂无

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

相关问题 在导轨中,给定Point对象的数组,我如何求和每个玩家的积分值? - In rails, given an array of Point objects, how do I sum the point values for each player? rails:对象数组,如何将对象属性的值求和? - rails: array of objects, how to sum values of object attributes? 如何从Rails 5应用程序中的表的特定属性列表中获取nil和not nil值的计数? - How do I get the count of nil and not nil values from a list of specific attributes from a table in a Rails 5 app? 当我在Rails中实例化对象时如何初始化属性? - How do I initialize attributes when I instantiate objects in Rails? 如何在Rails中显示来自其他表的对象属性 - how do i display an objects attributes from a different table in rails 在给定特定的经度/纬度的情况下,如何计算距纽约市附近地铁入口的距离? - How can I figure out the distance from nearby subway entrances in NYC given a specific latitude/longitude? 我怎么弄清楚为什么我的rails 3应用程序,使用mod_rails,是如此之慢? - How do I figure out why my rails 3 app, using mod_rails, is so slow? 如何仅在Rails中使用用户特定的对象? - How do I only use user specific objects in rails? 如何确定在Rails中随时打开哪些数据库连接? - How do I figure out what database connections are open at any one time in Rails? 我如何从 jquery-ui-rails gem 版本中找出 JQuery UI 的版本? - How do i figure out the version of JQuery UI from version of jquery-ui-rails gem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM