Possible Duplicate:
Getting index of an array’s element based on it’s properties
I’m working on creating an address book with HTML, jQuery, JS, CSS, all the normal stuff. It’s just a simple C.R.U.D. app, nothing fancy. I’m at an intermediate level and having a little bit of trouble with the method in which I’m going about creating it. I’m making each person in the book be an object. Each object is created with a name such as obj0. Then that object would be assigned values such as:
obj0.name = "Billy";
obj0.num = "1-207-233-8846";
obj0.address = "18 Phillmore Rd, Miami, Florida";
If any of those properties wouldn’t work then disregard it, I just made them up for the purpose of an example.
Now I’m realizing that I will most likely need to find which object has the name "Billy". I don’t want to make the user have to enter in obj0 to find Billy.
I’m naming the object obj0 rather than Billy because I want to make it possible to have more than one Billy in the address book.
Okay,now for the actual question:
How would I get the name or id (I want to get obj0 in case that’s unclear) of the object that has the name property of "Billy".
Assume that I have more than one object. The others would be called obj1 and obj2.
If possible I would also like to be able to search for the object with the number of "1-207-233-8446" or other phone numbers/addresses.
Why do you call the objects anything at all? obj0, obj1, etc…
Consider (unless you’re satisfied with the links to the right, which probably already answers this):
then you create an array of such objects:
and searching it becomes easy: