473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

acessing array elements via two different index methods

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[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
Jul 30 '07 #1
7 1345
On Jul 30, 12:36 pm, d d <dd_no_s...@please.netwrote:
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[i].name]=ra[i];

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.

Jul 30 '07 #2
On Jul 30, 9:36 am, d d <dd_no_s...@please.netwrote:
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
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

Jul 30 '07 #3
d d
David Mark wrote:
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
Jul 30 '07 #4
d d
Peter Michaux wrote:
>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
Jul 30 '07 #5
On Jul 30, 1:23 pm, d d <dd_no_s...@please.netwrote:
David Mark wrote:
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:
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

Jul 30 '07 #6
d d
Peter Michaux wrote:
>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 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
Jul 30 '07 #7
On Jul 31, 6:24 am, d d <dd_no_s...@please.netwrote:
Peter Michaux wrote:
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.
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

Jul 31 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.