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

Discussion in 'Javascript' started by mark4asp, Feb 28, 2007.

  1. mark4asp

    mark4asp Guest

    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).
    mark4asp, Feb 28, 2007
    #1
    1. Advertising

  2. mark4asp

    mark4asp Guest

    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));
    }

    On 28 Feb, 13:08, "mark4asp" <> wrote:
    > 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).[/color]
    mark4asp, Feb 28, 2007
    #2
    1. Advertising

  3. mark4asp

    VK Guest

    On Feb 28, 4:08 pm, "mark4asp" <> wrote:
    > 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?[/color]
    
    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"
    ]
    ];
    VK, Mar 1, 2007
    #3
  4. mark4asp

    mark4asp Guest

    On 28 Feb, 21:17, "David Golightly" <> wrote:
    > On Feb 28, 5:53 am, Duncan Booth <> wrote:
    >
    > > "mark4asp" <> wrote:
    > > > 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 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.
    mark4asp, Mar 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. I R BABOON
    Replies:
    0
    Views:
    313
    I R BABOON
    Oct 21, 2003
  2. TR
    Replies:
    3
    Views:
    1,213
  3. =?Utf-8?B?RGlmZmlkZW50?=
    Replies:
    3
    Views:
    821
    Juan T. Llibre
    May 13, 2005
  4. Replies:
    1
    Views:
    1,646
    Bjorn Abelli
    Apr 24, 2006
  5. Woodster
    Replies:
    6
    Views:
    357
    Woodster
    Oct 31, 2003
Loading...

Share This Page