I'm using an object to represent some data, and I have an array of
those objects. I need to determine if an object exists in the array
with a given identifier.
My object looks something like this:
var obj = {
id: 123,
val1: "hello",
val2: "world"
}
There are between 40-100 instances of obj in an Array. I need to know
if the array contains an object such that obj.id = 456.
I've looked at the Enumerable.collect() function, but since I need to
compare a large number of items against the array, I'm looking at n*x
run time.
I'm considering converting the Array to a Hash so I can test
"myHash[123]" to see if object 123 is in there. So I need a way to
convert the Array to an Hash such that the Hash's member keys are the
id property of each object as a value.
This might be my array:
[ {id: 10, val1: "hello world"},
{id: 22, val1: "hello world"},
{id: 16, val1: "hello world"},
{id: 87, val1: "hello world"} ]
And this is what I'd like to have as a Hash:
{ 10: {id: 10, val1: "hello world"},
22: {id: 22, val1: "hello world"},
16: {id: 16, val1: "hello world"},
87: {id: 87, val1: "hello world"} }
So far the best I've come up with is a collect iterator on the array
that builds a JSON string of each item. I then pass the
string.evalJSON() to the $H() operator.
Any better suggestions?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---