acessing array elements via two different index methods

D

d d

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.name]=ra;

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
 
D

David Mark

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

Don't use an array then. Use an object.
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.name]=ra;

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

[0]
[1]
[2]
["fred"]
["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.
 
P

Peter Michaux

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.name]=ra;

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


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 d

David said:
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.name]=ra;

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

~dd
 
D

d d

Peter said:
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
 
P

Peter Michaux

David said:
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.name]=ra;

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 d

Peter said:
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.

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
 
R

RobG

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top