Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / July 2007



Tip: Looking for answers? Try searching our database.

acessing array elements via two different index methods

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
d d - 30 Jul 2007 17:36 GMT
I have an array of objects that start out looking like this:

var ra=[
  {name:"fred",someproperty:"hello fred"},
  {name:"bob", someproperty:"hello bob"},
  {name:"joe", someproperty:"hello joe"}
];

I want to be able to access the array by index number from some code,
and by the name property from other code, as if it had been defined like
this instead:

var ra=[];
  ra["fred"] = {name:"fred",someproperty:"hello fred"};
  ra["bob"]  = {name:"bob", someproperty:"hello bob"};
  ra["joe"]  = {name:"joe", someproperty:"hello joe"};
];

I could choose either way, and use for(var x in ra) or use numerical
index and check the name property, but instead I did this:

var ra=[
  {name:"fred",someproperty:"hello fred"},
  {name:"bob", someproperty:"hello bob"},
  {name:"joe", someproperty:"hello joe"}
];
for(var i=0;i<ra.length;i++)
  ra[ra[i].name]=ra[i];

When I look at the array in visual studio I get this list:

[0]
[1]
[2]
["fred"]
["bob"]
["joe"]

and if I make a change to the contents of [0], they're reflected in the
["joe"] entry because ["joe"] is just a pointer to [0].

Perfect solution or a bad idea? I'll never have more than 10 in the
original array and it's not involved in any major heavy loops.

~dd
David Mark - 30 Jul 2007 19:11 GMT
> I have an array of objects that start out looking like this:
>
[quoted text clipped - 6 lines]
> I want to be able to access the array by index number from some code,
> and by the name property from other code, as if it had been defined like

Don't use an array then.  Use an object.

> this instead:
>
[quoted text clipped - 23 lines]
> ["bob"]
> ["joe"]

Fred, Bob and Joy are new properties of the Array object.  The length
of this array is 3, which isn't very intuitive.
d d - 30 Jul 2007 21:23 GMT
> Fred, Bob and Joy are new properties of the Array object.  The length
> of this array is 3, which isn't very intuitive.

I thought that was the best part about it. The request came to me as
"you know how we've got this array of objects each of which has a name
property? .... well it would be cool if we could index into the array by
that name ... we want it to stay as it is currently, but would like this
extra feature".

This simple loop creates the named properties which can then be used to
access the array directly by name, e.g.: ra["joe"]

for(var i=0;i<ra.length;i++)
  ra[ra[i].name]=ra[i];

It seemed perfect to me, but I should have known not to bring it here:

~dd
Peter Michaux - 30 Jul 2007 21:32 GMT
> > Fred, Bob and Joy are new properties of the Array object.  The length
> > of this array is 3, which isn't very intuitive.
[quoted text clipped - 12 lines]
>
> It seemed perfect to me, but I should have known not to bring it here:

This seems like a Rube Goldberg was involved in the design. Just using
an plain object is a better option. Then you can use for-in loop to
iterate through the properties.

Peter
d d - 30 Jul 2007 21:53 GMT
>> The request came to me as
>> "you know how we've got this array of objects each of which has a name
[quoted text clipped - 5 lines]
> an plain object is a better option. Then you can use for-in loop to
> iterate through the properties.

Of course using a plain object is a better design option, and a for-in
loop would iterate through it, but this array isn't at the design stage.
This array already exists.

There's already a lot of code in different places (that they don't want
changing) which loops through it numerically and has certain expectations.

My brief was to offer a solution that would allow them to index into the
array by the name property that each array object has, but without
changing the nature of the array. All the existing code had to continue
working and find everything where it used to be. The array length also
had to remain the same. I managed to do this without creating a separate
lookup array, or a separate lookup function. Just one simple loop that
could be executed from anywhere and would ADD info to the array object
that gives the dual functionality they wanted.

~dd
Peter Michaux - 30 Jul 2007 19:46 GMT
> I have an array of objects that start out looking like this:
>
[quoted text clipped - 41 lines]
>
> ~dd

For the types of questions you are asking, you might find you get a
better feeling for JavaScript if you read a book. See

http://www.jibbering.com/faq/#FAQ3_1

Peter
d d - 30 Jul 2007 21:24 GMT
>> Perfect solution or a bad idea? I'll never have more than 10 in the
>> original array and it's not involved in any major heavy loops.
>> ~dd
> For the types of questions you are asking, you might find you get a
> better feeling for JavaScript if you read a book. See

OK, I'll buy a book. Thanks for the suggestion. I'm sure they all go
into great detail about ideas like this.

I really don't know why I come here.

(Don't all shout at once "we wish you'd stop")...

~dd
RobG - 31 Jul 2007 03:27 GMT
> >> Perfect solution or a bad idea? I'll never have more than 10 in the
> >> original array and it's not involved in any major heavy loops.
[quoted text clipped - 6 lines]
>
> I really don't know why I come here.

I do - you get good advice (sometimes painfully breif, other times
abrupt and occasionally with a does of sarcasm - but somewhere in
there will be a gem).

> (Don't all shout at once "we wish you'd stop")...

The silence is deafening :-)

The only silly questions are the ones that aren't asked.  Even really
dumb questions are useful if they elicit a good response or discussion
of some interesting point.  Some of the most useful discsussions here
have been in response to posts that one might right-off as muddle-
headed or completely absurd.  I don't think you're in that league yet.

--
Rob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.