Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数组:
["Passive: 8", "Passive: 9", "Neutral: 3"]
现在我需要计算Passive被看到的次数。如何计算子字符串在数组内的字符串中出现的次数?
Passive
["Passive: 8", "Passive: 9", "Neutral: 3"].grep(/Passive/).count # => 2
我在里面找到了答案grep。
grep
arr.grep(/^Passive/).each do |element| ... end
这足以满足我的目的。
另一种方法:
array.count{|x|x['Passive']}