Collections in javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eitan

    Collections in javascript

    Hello,

    I am looking for a collection object in javascript (like dictionary in VB).

    Give me some sample code ,please.

    Thanks :)


  • David Dorward

    #2
    Re: Collections in javascript

    Eitan wrote:
    [color=blue]
    > I am looking for a collection object in javascript (like dictionary in
    > VB).[/color]

    var foo = new Array();
    foo['bar'] = 'baz';


    --
    David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is

    Comment

    • Grant Wagner

      #3
      Re: Collections in javascript

      David Dorward wrote:
      [color=blue]
      > Eitan wrote:
      >[color=green]
      > > I am looking for a collection object in javascript (like dictionary in
      > > VB).[/color]
      >
      > var foo = new Array();
      > foo['bar'] = 'baz';[/color]

      Don't use an Array, because if you attempt to iterate the properties of the
      object later, you'll end up with properties called 'pop', 'push', 'length',
      etc

      var foo = new Object(); // { }
      foo['bar'] = 'baz';

      Even with this, there are a number of enumerable properties you'll uncover
      using -for (var x in foo)-.

      If you actually need a Collection object you'll have to roll your own. For
      example, the following code give you a Collection which guarantees that values
      are retrieved in the order they were added:

      <script type="text/javascript">
      function Collection() {
      var collection = {};
      var order = [];

      this.add = function(proper ty, value) {
      if (!this.exists(p roperty)) {
      collection[property] = value;
      order.push(prop erty);
      }
      }
      this.remove = function(proper ty) {
      collection[property] = null;
      var ii = order.length;
      while (ii-- > 0) {
      if (order[ii] == property) {
      order[ii] = null;
      break;
      }
      }
      }
      this.toString = function() {
      var output = [];
      for (var ii = 0; ii < order.length; ++ii) {
      if (order[ii] != null) {
      output.push(col lection[order[ii]]);
      }
      }
      return output;
      }
      this.getKeys = function() {
      var keys = [];
      for (var ii = 0; ii < order.length; ++ii) {
      if (order[ii] != null) {
      keys.push(order[ii]);
      }
      }
      return keys;
      }
      this.update = function(proper ty, value) {
      if (value != null) {
      collection[property] = value;
      }
      var ii = order.length;
      while (ii-- > 0) {
      if (order[ii] == property) {
      order[ii] = null;
      order.push(prop erty);
      break;
      }
      }
      }
      this.exists = function(proper ty) {
      return collection[property] != null;
      }
      }

      var myCollection = new Collection();
      myCollection.ad d("first", "Adam");
      myCollection.ad d("second", "Eve");
      myCollection.ad d("third", "Cane");
      myCollection.ad d("fourth", "Abel");
      myCollection.re move("second");
      myCollection.ad d("fifth", "Noah");
      alert(myCollect ion.toString()) ;
      alert(myCollect ion.getKeys());
      myCollection.up date("first");
      alert(myCollect ion.toString()) ;
      alert(myCollect ion.exists("thi rd"));
      alert(myCollect ion.exists("som ething"));
      </script>

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Steve van Dongen [MSFT]

        #4
        Re: Collections in javascript

        "Eitan" <no_spam_please @nospam.com> wrote:
        [color=blue]
        >Hello,
        >
        >I am looking for a collection object in javascript (like dictionary in VB).
        >
        >Give me some sample code ,please.
        >
        >Thanks :)[/color]

        If you are only interested in MS JScript you can use the dictionary
        object.
        Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


        Regards,
        Steve
        --
        Please post questions to the newsgroup; everyone benefits.
        This post is provided "AS IS" with no warranties, and confers no rights
        Sample code subject to http://www.microsoft.com/info/cpyright.htm

        Comment

        Working...