Find value in an array

Viewed 246828

In Ruby, how can I find a value in an array?

11 Answers

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").

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);
Related