Reputation: 3927
In Ruby, how can I find a value in an array?
Upvotes: 166
Views: 277411
Reputation: 6145
If you want find one value from the Array, use Array#find
:
arr = [1,2,6,4,9]
arr.find {|e| e % 3 == 0} #=> 6
If you want to find multiple values from the Array, use Array#select
:
arr.select {|e| e % 3 == 0} #=> [ 6, 9 ]
e.include? 6 #=> true
To find if a value exists in an Array you can also use #in?
when using ActiveSupport. #in?
works for any object that responds to #include?
:
arr = [1, 6]
6.in? arr #=> true
Upvotes: 61
Reputation: 7
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let h = arr.find(x => x.name == 'string 1') ?? arr[0]
console.log(h);
Upvotes: -3
Reputation: 5974
If you're trying to determine whether a certain value exists inside an array, you can use Array#include?(value):
a = [1,2,3,4,5]
a.include?(3) # => true
a.include?(9) # => false
If you mean something else, check the Ruby Array API
Upvotes: 134
Reputation: 623
You can go for array methods.
To see all array methods use methods
function with array.
For Example,
a = ["name", "surname"]
a.methods
By the way you can use different method for checking value in array
You can use a.include?("name")
.
Upvotes: 2
Reputation: 1381
Using Array#select
will give you an array of elements that meet the criteria. But if you're looking for a way of getting the element out of the array that meets your criteria, Enumerable#detect
would be a better way to go:
array = [1,2,3]
found = array.select {|e| e == 3} #=> [3]
found = array.detect {|e| e == 3} #=> 3
Otherwise you'd have to do something awkward like:
found = array.select {|e| e == 3}.first
Upvotes: 128
Reputation: 18613
I know this question has already been answered, but I came here looking for a way to filter elements in an Array based on some criteria. So here is my solution example: using select
, I find all constants in Class that start with "RUBY_"
Class.constants.select {|c| c.to_s =~ /^RUBY_/ }
UPDATE: In the meantime I have discovered that Array#grep works much better. For the above example,
Class.constants.grep /^RUBY_/
did the trick.
Upvotes: 8
Reputation: 11429
This answer is for everyone that realizes the accepted answer does not address the question as it currently written.
The question asks how to find a value in an array. The accepted answer shows how to check whether a value exists in an array.
There is already an example using index
, so I am providing an example using the select
method.
1.9.3-p327 :012 > x = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
1.9.3-p327 :013 > x.select {|y| y == 1}
=> [1]
Upvotes: 10
Reputation: 7312
Like this?
a = [ "a", "b", "c", "d", "e" ]
a[2] + a[0] + a[1] #=> "cab"
a[6] #=> nil
a[1, 2] #=> [ "b", "c" ]
a[1..3] #=> [ "b", "c", "d" ]
a[4..7] #=> [ "e" ]
a[6..10] #=> nil
a[-3, 3] #=> [ "c", "d", "e" ]
# special cases
a[5] #=> nil
a[5, 1] #=> []
a[5..10] #=> []
or like this?
a = [ "a", "b", "c" ]
a.index("b") #=> 1
a.index("z") #=> nil
Upvotes: 24
Reputation: 3927
Thanks for replies.
I did like this:
puts 'find' if array.include?(value)
Upvotes: 0
Reputation: 12092
Use:
myarray.index "valuetoFind"
That will return you the index of the element you want or nil if your array doesn't contain the value.
Upvotes: 17