473,804 Members | 3,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I use name:value pairs like an array?

How can I use name:value pairs like an array?

1. I want to load data on to the page which will be used to populate a
list box (see example below):

<html>
<head>
<script type="text/javascript">
var aInvestor = {1448:"4Imprint Group plc Pension Scheme",
1061:"Aargau Pensionskasse", 659:"ABB Koncernens Pensionsstiftel se",
44:"ABB Pensionskasse", 387:"Abbey National PLC Amalgamated Pension
Funds",1092:"AB Bs Pensjonskasse"}
function init()
{
for (var code in aInvestor)
document.getEle mentById('selII ').options.add( new
Option(aInvesto r[code],code));
}

</script>

</head>
<body onload="init(); ">
<select size="6" name="selII" id="selII"></select>
</body>
</html>

2. I also want to be able to carry out a binary search on the text in
the object (aInvestor).

The final purpose of this is to allow the user to do an incremental
search on the aInvestor items such that only those items are currently
shown in the list box.

eg. typing "a" will show all items beginning with "a", typing "ab"
shows only all those items beginning with "ab", etc.

The aInvestor is populated on the server.

My problem is that the aInvestor doesn't have a length property and I
need that to do my binary search - how can I get around this problem?

PS: The code need only work with IE (as there are only about 1000
clients who are all corporate microserfs).

Feb 28 '07 #1
7 12726
OK. I have figured the quick fix.

Get rid of the object and use two arrays instead. Most likely this
will be the fastest to implement and speed is of some importance here.

var Icode = new Array(1448,1061 ,659,44,387,109 2)
var Iname = new Array("4Imprint Group plc Pension Scheme","Aargau
Pensionskasse", "ABB Koncernens Pensionsstiftel se","ABB
Pensionskasse", "Abbey National PLC Amalgamated Pension Funds","ABBs
Pensjonskasse")

function init()
{
for (var i=0; i < Icode.length; i++)
document.getEle mentById('selII ').options.add( new
Option(Iname[i],Icode[i]));
}

On 28 Feb, 13:08, "mark4asp" <mark4...@gmail .comwrote:
How can I use name:value pairs like an array?

1. I want to load data on to the page which will be used to populate a
list box (see example below):

<html>
<head>
<script type="text/javascript">
var aInvestor = {1448:"4Imprint Group plc Pension Scheme",
1061:"Aargau Pensionskasse", 659:"ABB Koncernens Pensionsstiftel se",
44:"ABB Pensionskasse", 387:"Abbey National PLC Amalgamated Pension
Funds",1092:"AB Bs Pensjonskasse"}
function init()
{
for (var code in aInvestor)
document.getEle mentById('selII ').options.add( new
Option(aInvesto r[code],code));
}

</script>

</head>
<body onload="init(); ">
<select size="6" name="selII" id="selII"></select>
</body>
</html>

2. I also want to be able to carry out a binary search on the text in
the object (aInvestor).

The final purpose of this is to allow the user to do an incremental
search on the aInvestor items such that only those items are currently
shown in the list box.

eg. typing "a" will show all items beginning with "a", typing "ab"
shows only all those items beginning with "ab", etc.

The aInvestor is populated on the server.

My problem is that the aInvestor doesn't have a length property and I
need that to do my binary search - how can I get around this problem?

PS: The code need only work with IE (as there are only about 1000
clients who are all corporate microserfs).

Feb 28 '07 #2
"mark4asp" <ma******@gmail .comwrote:
2. I also want to be able to carry out a binary search on the text in
the object (aInvestor).

The final purpose of this is to allow the user to do an incremental
search on the aInvestor items such that only those items are currently
shown in the list box.

eg. typing "a" will show all items beginning with "a", typing "ab"
shows only all those items beginning with "ab", etc.
Is this going to be better than the standard behaviour of a SELECT
control where e.g. typing "a" will jump you to the first entry beginning
with an "a", typing "ab" will find the first entry beginning with "ab"?
It's true that the other entries are still there but reloading the
content of a long list with javascript isn't going to be fast.

It sounds to me like you are trying to duplicate existing functionality
in a less user-friendly manner.

Anyway, to answer your question, there is nothing built in to Javascript
objects to let you search the attributes. Also note that there is no
defined order to the attributes, so although it may appear that Internet
Explorer lets you iterate over them in the order they appeared in your
source file that may not always be the case and is a bug waiting to bite
you.

I suggest that if you really want to do this you start by defining your
data as an array. That way you get a defined order and a length both
readily accessible:

var aInvestor = [
[1448,"4Imprint Group plc Pension Scheme"],
[1061,"Aargau Pensionskasse"],
[659,"ABB Koncernens Pensionsstiftel se"],
[44,"ABB Pensionskasse"],
[387,"Abbey National PLC Amalgamated Pension Funds"],
[1092,"ABBs Pensjonskasse"]];

Better still, put the values directly into the SELECT in the HTML and if
you want you can copy the values out into an array from your script.
This means that the SELECT comes up already populated and doesn't need
to be populated from javascript before it can be used.
Feb 28 '07 #3
On Feb 28, 5:53 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
"mark4asp" <mark4...@gmail .comwrote:
2. I also want to be able to carry out a binary search on the text in
the object (aInvestor).
The final purpose of this is to allow the user to do an incremental
search on the aInvestor items such that only those items are currently
shown in the list box.
eg. typing "a" will show all items beginning with "a", typing "ab"
shows only all those items beginning with "ab", etc.

Is this going to be better than the standard behaviour of a SELECT
control where e.g. typing "a" will jump you to the first entry beginning
with an "a", typing "ab" will find the first entry beginning with "ab"?
It's true that the other entries are still there but reloading the
content of a long list with javascript isn't going to be fast.

It sounds to me like you are trying to duplicate existing functionality
in a less user-friendly manner.

Anyway, to answer your question, there is nothing built in to Javascript
objects to let you search the attributes. Also note that there is no
defined order to the attributes, so although it may appear that Internet
Explorer lets you iterate over them in the order they appeared in your
source file that may not always be the case and is a bug waiting to bite
you.

I suggest that if you really want to do this you start by defining your
data as an array. That way you get a defined order and a length both
readily accessible:

var aInvestor = [
[1448,"4Imprint Group plc Pension Scheme"],
[1061,"Aargau Pensionskasse"],
[659,"ABB Koncernens Pensionsstiftel se"],
[44,"ABB Pensionskasse"],
[387,"Abbey National PLC Amalgamated Pension Funds"],
[1092,"ABBs Pensjonskasse"]];

Better still, put the values directly into the SELECT in the HTML and if
you want you can copy the values out into an array from your script.
This means that the SELECT comes up already populated and doesn't need
to be populated from javascript before it can be used.
You can do that, or you can do:

var aInvestor = {
1448:"4Imprint Group plc Pension Scheme",
1061:"Aargau Pensionskasse",
659:"ABB Koncernens Pensionsstiftel se",
44:"ABB Pensionskasse",
387:"Abbey National PLC Amalgamated Pension Funds",
1092:"ABBs Pensjonskasse"} ;

var sel = document.getEle mentById('selII ');
for (var n in aInvestor) {
sel.options.add (new Option(aInvesto r[n], n));
}

-David

Feb 28 '07 #4
"David Golightly" <da******@gmail .comwrote:
On Feb 28, 5:53 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
>Also note that there is no defined order to the attributes, so
although it may appear that Internet Explorer lets you iterate over
them in the order they appeared in your source file that may not
always be the case and is a bug waiting to bite you.
.... snip ...
>
You can do that, or you can do:

var aInvestor = {
1448:"4Imprint Group plc Pension Scheme",
1061:"Aargau Pensionskasse",
659:"ABB Koncernens Pensionsstiftel se",
44:"ABB Pensionskasse",
387:"Abbey National PLC Amalgamated Pension Funds",
1092:"ABBs Pensjonskasse"} ;

var sel = document.getEle mentById('selII ');
for (var n in aInvestor) {
sel.options.add (new Option(aInvesto r[n], n));
}
How is that better or even significantly different than the OP's code?

Was some part of 'no defined order to the attributes' unclear?

I believe that IE will appear to iterate through the attributes in the
order in which they are originally added to the object but this is not
part of the ecmascript definition (nor even JScript's documentation), so
other implementations , including possibly future versions of IE could
iterate in a different order. There is simply no need to be relying on
this undefined behaviour as arrays work better for this use case anyway.

Even Microsoft's documentation (since the OP was only concerned about
IE) says:

When iterating over an object, there is no way to determine or control
the order in which the member names of the object are assigned to
variable.
Mar 1 '07 #5
On Mar 1, 1:18 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
"David Golightly" <davig...@gmail .comwrote:
On Feb 28, 5:53 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
Also note that there is no defined order to the attributes, so
although it may appear that Internet Explorer lets you iterate over
them in the order they appeared in your source file that may not
always be the case and is a bug waiting to bite you.
... snip ...
You can do that, or you can do:
var aInvestor = {
1448:"4Imprint Group plc Pension Scheme",
1061:"Aargau Pensionskasse",
659:"ABB Koncernens Pensionsstiftel se",
44:"ABB Pensionskasse",
387:"Abbey National PLC Amalgamated Pension Funds",
1092:"ABBs Pensjonskasse"} ;
var sel = document.getEle mentById('selII ');
for (var n in aInvestor) {
sel.options.add (new Option(aInvesto r[n], n));
}

How is that better or even significantly different than the OP's code?

Was some part of 'no defined order to the attributes' unclear?

I believe that IE will appear to iterate through the attributes in the
order in which they are originally added to the object but this is not
part of the ecmascript definition (nor even JScript's documentation), so
other implementations , including possibly future versions of IE could
iterate in a different order. There is simply no need to be relying on
this undefined behaviour as arrays work better for this use case anyway.

Even Microsoft's documentation (since the OP was only concerned about
IE) says:

When iterating over an object, there is no way to determine or control
the order in which the member names of the object are assigned to
variable.
Oops. I started reading with the second post. My mistake. -d

Mar 1 '07 #6
VK
On Feb 28, 4:08 pm, "mark4asp" <mark4...@gmail .comwrote:
How can I use name:value pairs like an array?

1. I want to load data on to the page which will be used to populate a
list box (see example below):

<html>
<head>
<script type="text/javascript">
var aInvestor = {1448:"4Imprint Group plc Pension Scheme",
1061:"Aargau Pensionskasse", 659:"ABB Koncernens Pensionsstiftel se",
44:"ABB Pensionskasse", 387:"Abbey National PLC Amalgamated Pension
Funds",1092:"AB Bs Pensjonskasse"}
function init()
{
for (var code in aInvestor)
document.getEle mentById('selII ').options.add( new
Option(aInvesto r[code],code));
}

</script>

</head>
<body onload="init(); ">
<select size="6" name="selII" id="selII"></select>
</body>
</html>

2. I also want to be able to carry out a binary search on the text in
the object (aInvestor).

The final purpose of this is to allow the user to do an incremental
search on the aInvestor items such that only those items are currently
shown in the list box.

eg. typing "a" will show all items beginning with "a", typing "ab"
shows only all those items beginning with "ab", etc.

The aInvestor is populated on the server.

My problem is that the aInvestor doesn't have a length property and I
need that to do my binary search - how can I get around this problem?
One of ideas:

var aInvestor = [
[
{1448:"4Imprint Group plc Pension Scheme"},
{1061:"Aargau Pensionskasse"} ,
{659:"ABB Koncernens Pensionsstiftel se"},
{44:"ABB Pensionskasse"} ,
{387:"Abbey National PLC Amalgamated Pension Funds",
{1092:"ABBs Pensjonskasse"}
],
[
// companies on "B"
],
// ...
[
// companies on "Z"
]
];

Mar 1 '07 #7
On 28 Feb, 21:17, "David Golightly" <davig...@gmail .comwrote:
On Feb 28, 5:53 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
"mark4asp" <mark4...@gmail .comwrote:
2. I also want to be able to carry out a binary search on the text in
the object (aInvestor).
The final purpose of this is to allow the user to do an incremental
search on the aInvestor items such that only those items are currently
shown in the list box.
eg. typing "a" will show all items beginning with "a", typing "ab"
shows only all those items beginning with "ab", etc.
Is this going to be better than the standard behaviour of a SELECT
control where e.g. typing "a" will jump you to the first entry beginning
with an "a", typing "ab" will find the first entry beginning with "ab"?
It's true that the other entries are still there but reloading the
content of a long list with javascript isn't going to be fast.
It sounds to me like you are trying to duplicate existing functionality
in a less user-friendly manner.
Anyway, to answer your question, there is nothing built in to Javascript
objects to let you search the attributes. Also note that there is no
defined order to the attributes, so although it may appear that Internet
Explorer lets you iterate over them in the order they appeared in your
source file that may not always be the case and is a bug waiting to bite
you.
I suggest that if you really want to do this you start by defining your
data as an array. That way you get a defined order and a length both
readily accessible:
var aInvestor = [
[1448,"4Imprint Group plc Pension Scheme"],
[1061,"Aargau Pensionskasse"],
[659,"ABB Koncernens Pensionsstiftel se"],
[44,"ABB Pensionskasse"],
[387,"Abbey National PLC Amalgamated Pension Funds"],
[1092,"ABBs Pensjonskasse"]];
Better still, put the values directly into the SELECT in the HTML and if
you want you can copy the values out into an array from your script.
This means that the SELECT comes up already populated and doesn't need
to be populated from javascript before it can be used.

You can do that, or you can do:

var aInvestor = {
1448:"4Imprint Group plc Pension Scheme",
1061:"Aargau Pensionskasse",
659:"ABB Koncernens Pensionsstiftel se",
44:"ABB Pensionskasse",
387:"Abbey National PLC Amalgamated Pension Funds",
1092:"ABBs Pensjonskasse"} ;

var sel = document.getEle mentById('selII ');
for (var n in aInvestor) {
sel.options.add (new Option(aInvesto r[n], n));

}
David - I decided not to use the 'associative array' method (above)
because I need to know the count of the number of items and there is
no (simple) way to get the count of the items in an associative array.

I need the count because I will be doing a binary search variant to
get the minimum and maximum indices for the current incremental search
value.

Duncan's version of the array looks better for that.

I share everyone's suspicion of reinventing client controls to do what
they're not suppossed to do. A pity my boss doesn't.

This data has about 1600 items and the text alone is about 48K. At the
moment it is just one long pipe (|) delimited string that is searched
using code taken from the O'Reilly Regular Expression book - which
surely must be some kind of linear search - Now theory tells me that
there will be a maximum of 11 comparisons to do a binary search on
1600 items - so the current search method must go and the data
structure (or lack of structure) should go too.

The page hangs about for about 2 seconds when the first letter is
entered into the incremental search or when the page is first loaded.

Clearly it's a good idea to start with the select already populated
but it may also be a good idea to have the array ready at hand too -
given that only the items in the incremental search are to be seen in
the select list.

That's how it currently works (above) - I just want to make it go
faster - there's no way I can change the functionality.

I'll use Duncan's suggested array.

Mar 5 '07 #8

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

Similar topics

7
5374
by: Woodster | last post by:
Further to a question I posted here recently about coding an xml rpeort parser, I need to use a list of name/value pairs, in this case, a list property names and values for a given tag. Rather than deriving a linked list class and adding in 2 char * to the derived class, I have decided to try and start using templates howver not having used templates before I do not know where to start. Unfortunately time is not on my side and I was...
4
12672
by: Bill | last post by:
If, for example, I retrieve a connectionstring from a config file using something like: Value = ConfigurationSettings.AppSettings; This will return a string that is semi-colon delimited. If I want, say, to retrieve the password from this string will I need to explicity parse it?
16
1512
by: Bush is a Fascist | last post by:
Hi all, What do most languages call a name-value pairing? Or perhaps my question should be, why not just call it a name-value pairing? Too many syllables? Did Knuth invent a handy term for such a thing? Thanks.
2
8427
by: Bill Todd | last post by:
Is there an object in the .NET framework that has methods for working with Name=Value pairs such as those in a connection string? What I am looking for is something similar to an ArrayList but with an IndexOfName method that will find the first occurance of the value on the left of the = sign and that also has a way to change the Value on the right side. -- Bill
1
1373
by: Isz | last post by:
Dear group: I am inexperienced in more advanced csharp techniques such as interfaces/interfaces/collections etc. But here is what I would like to do: I have an object in MM Flash that contains several name-value pairs of different type. Something like this... var recData = ;
1
3052
by: Bill | last post by:
I have strings returned from a config file that contain zero or more name/value pairs using a pattern like "name = value;" I want to extract the value of a given name (passwords, etc.). I want this to ignore whitespace in the pattern match and not be case sensitive. Anyone have a pattern that will do this?
3
1682
by: TR | last post by:
A company that lets affiliates search its database assumes that affiliates will paste code, which looks something like this, into their web pages: <form method="GET" name="search_5" action="http://www.somedomain.com/articles/results.xml" target="_blank"> <!-- Hidden values to be passed to the stored proc --> <input type="hidden" name="XTYPE" value="FR"> <input type="hidden" name="TG" value="1"> <input type="hidden" name="CRW"...
3
1573
by: Diffident | last post by:
Hello All, I want to display all the name-value pairs of the session object....how can I print them out? i.e., suppose once a user logs in I set a session variable login time Session = "2pm" and Session="10pm". So how can I print these variables i.e., logintime and logouttime and their values together? In which namespace is the Session object...can I retrieve the name-value pairs as collection?
6
1825
by: Andy Sutorius via DotNetMonster.com | last post by:
Hi, What collection can I use to store name value pairs that contain duplicates in both columns? Thanks, Andy --
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.