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

M

mark4asp

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 Pensionsstiftelse",
44:"ABB Pensionskasse",387:"Abbey National PLC Amalgamated Pension
Funds",1092:"ABBs Pensjonskasse"}
function init()
{
for (var code in aInvestor)
document.getElementById('selII').options.add(new
Option(aInvestor
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).
 
M

mark4asp

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,1092)
var Iname = new Array("4Imprint Group plc Pension Scheme","Aargau
Pensionskasse","ABB Koncernens Pensionsstiftelse","ABB
Pensionskasse","Abbey National PLC Amalgamated Pension Funds","ABBs
Pensjonskasse")

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

VK

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 Pensionsstiftelse",
44:"ABB Pensionskasse",387:"Abbey National PLC Amalgamated Pension
Funds",1092:"ABBs Pensjonskasse"}
function init()
{
for (var code in aInvestor)
document.getElementById('selII').options.add(new
Option(aInvestor
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?[/QUOTE]

One of ideas:

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

mark4asp

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 Pensionsstiftelse"],
[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 Pensionsstiftelse",
44:"ABB Pensionskasse",
387:"Abbey National PLC Amalgamated Pension Funds",
1092:"ABBs Pensjonskasse"};

var sel = document.getElementById('selII');
for (var n in aInvestor) {
sel.options.add(new Option(aInvestor[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.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top