Possible Duplicate:
Find object by id in array of javascript objects
I can do this to get the particular hash from an array of hash where a particular key has a certain value. For example, if I want to get the name
value of hash where key id
is 2, I can do this:
array = [{:id => 1, :name => "A"},
{:id => 3, :name => "C"},
{:id => 2, :name => "B"}]
id_2_hash = array.detect {|a| a[:id] == 2}
=> {:id => 2, :name => "B"}
id_2_hash[:name]
=> "B"
I want the same to be done to a JSON object like this:
[
{
'id': 1,
'name': 'A'
},
{
'id': 3,
'name': 'C'
},
{
'id': 2,
'name': 'B'
}
]
How can I do this in Javascript/Jquery?