removing array item

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

    removing array item

    I have the following. There must be something less cumbersome without push
    pop. The function parameter obj is the event.srcElemen t and has the
    attributes(prop erties?) picNum and alb pinned to it. The function is part of
    a process to delete the selected obj, a photo in this case from the album
    vis albumArr. TIA
    Jimbo
    function Album(albumName ) {
    this.name=album Name;
    this.paths=new Array();
    }
    var albumArr(); // holds an array of Albums();
    function removeArrSrc(ob j) {
    var dummy=new Array();
    for(i=0;i<album Arr.length;i++) {
    dummy[i]=new Album(albumArr[i].name);
    var el=albumArr[i].paths;
    for(var j=0;j<el.length ;j++) {
    if(dummy[i].name==obj.alb) {
    if(j==obj.picNu m) continue;
    }
    var len=dummy[i].paths.length;
    dummy[i].paths[len]=el[j];
    }
    }
    albumArr=dummy;
    }





  • Michael Winter

    #2
    Re: removing array item

    On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale <photom@netvisi on.net.il>
    wrote:

    012345678901234 567890123456789 012345678901234 567890123456789 0123456789[color=blue]
    > I have the following. There must be something less cumbersome without
    > push pop. The function parameter obj is the event.srcElemen t[/color]

    Is this for the Web, or just an IE-only environment?
    [color=blue]
    > and has the attributes(prop erties?) picNum and alb pinned to it. The
    > function is part of a process to delete the selected obj, a photo in
    > this case from the album vis albumArr. TIA[/color]
    [color=blue]
    > function Album(albumName ) {
    > this.name=album Name;
    > this.paths=new Array();
    > }
    > var albumArr(); // holds an array of Albums();[/color]

    I don't know what that's supposed to be, but I'm certain it'll cause an
    error on most browsers. Perhaps you meant:

    var albumArr = [];
    [color=blue]
    > function removeArrSrc(ob j) {
    > var dummy=new Array();
    > for(i=0;i<album Arr.length;i++) {
    > dummy[i]=new Album(albumArr[i].name);
    > var el=albumArr[i].paths;
    > for(var j=0;j<el.length ;j++) {
    > if(dummy[i].name==obj.alb) {
    > if(j==obj.picNu m) continue;
    > }
    > var len=dummy[i].paths.length;
    > dummy[i].paths[len]=el[j];
    > }
    > }
    > albumArr=dummy;
    > }[/color]

    How about:

    function removeArrSrc(o) {
    var i = 0, n = albumArr.length ;

    // Find the element, 'o'
    while(i < n && albumArr[i] != o) {++i;}
    // If 'o' exists in the array, 'i' will now point it
    if(i < n) {
    // Don't want to reach the last element
    --n;
    // Now shift the remaining elements down
    while(i < n) {albumArr[i] = albumArr[i + 1]; ++i;}
    // Finally, truncate the array
    albumArr.length = n;
    }
    }

    which is removes the element in-place. You could also use:

    function removeArrSrc(o) {
    var i = 0, n = albumArr.length ;

    // Find the element, 'o'
    while(i < n && albumArr[i] != o) {++i;}
    // If 'o' exists in the array, 'i' will now point it
    if(i < n) {
    albumArr = albumArr.slice( 0, i).concat(album Arr.slice(i + 1));
    }
    }

    which might be a little quicker (due to being performed by the browser),
    but probably isn't (due to the resulting array allocations).

    I haven't tested these, by the way.

    Good luck,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • J. J. Cale

      #3
      Re: removing array item


      "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
      news:opsdjinqrx x13kvk@atlantis ...[color=blue]
      > On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale <photom@netvisi on.net.il>
      > wrote:
      >
      > 012345678901234 567890123456789 012345678901234 567890123456789 0123456789[color=green]
      > > I have the following. There must be something less cumbersome without
      > > push pop. The function parameter obj is the event.srcElemen t[/color]
      >
      > Is this for the Web, or just an IE-only environment?
      >[/color]

      Ideally it would be generic although currently I'm writing an HTA.
      [color=blue][color=green]
      > > and has the attributes(prop erties?) picNum and alb pinned to it. The
      > > function is part of a process to delete the selected obj, a photo in
      > > this case from the album vis albumArr. TIA[/color]
      >[color=green]
      > > function Album(albumName ) {
      > > this.name=album Name;
      > > this.paths=new Array();[/color][/color]

      paths array holds image paths
      [color=blue][color=green]
      > > }
      > > var albumArr(); // holds an array of Albums();[/color]
      >
      > I don't know what that's supposed to be, but I'm certain it'll cause an
      > error on most browsers. Perhaps you meant:
      >
      > var albumArr = [];
      >[/color]
      Sorry! It is declared var albumArr=new Array();[color=blue][color=green]
      > > function removeArrSrc(ob j) {
      > > var dummy=new Array();
      > > for(i=0;i<album Arr.length;i++) {
      > > dummy[i]=new Album(albumArr[i].name);
      > > var el=albumArr[i].paths;
      > > for(var j=0;j<el.length ;j++) {
      > > if(dummy[i].name==obj.alb) {
      > > if(j==obj.picNu m) continue;
      > > }
      > > var len=dummy[i].paths.length;
      > > dummy[i].paths[len]=el[j];
      > > }
      > > }
      > > albumArr=dummy;
      > > }[/color]
      >
      > How about:
      >
      > function removeArrSrc(o) {
      > var i = 0, n = albumArr.length ;
      >
      > // Find the element, 'o'
      > while(i < n && albumArr[i] != o) {++i;}[/color]
      the array albumArr contains an array of Album objects. I need to delete
      the path for one image.
      the reference obj (here 'o') has the album name and the index in the
      paths array pinned as attributes
      I'm searching therefore for o.alb (the album name) and o.picNum the
      offset into the paths array of the Album object.
      while(i<n && albumArr[i].name != o.alb) {++i} should work to
      find the album
      [color=blue]
      > // If 'o' exists in the array, 'i' will now point it
      > if(i < n) {
      > // Don't want to reach the last element
      > --n;
      > // Now shift the remaining elements down
      > while(i < n) {albumArr[i] = albumArr[i + 1]; ++i;}
      > // Finally, truncate the array
      > albumArr.length = n;
      > }
      > }
      >[/color]
      This would delete the entire album. I need to delete the image from the
      paths array of the album. The slice method with a regex is what I'm looking
      for but I'm just getting into regex.
      [color=blue]
      > which is removes the element in-place. You could also use:
      >
      > function removeArrSrc(o) {
      > var i = 0, n = albumArr.length ;
      >
      > // Find the element, 'o'
      > while(i < n && albumArr[i] != o) {++i;}
      > // If 'o' exists in the array, 'i' will now point it
      > if(i < n) {
      > albumArr = albumArr.slice( 0, i).concat(album Arr.slice(i + 1));
      > }
      > }
      >
      > which might be a little quicker (due to being performed by the browser),
      > but probably isn't (due to the resulting array allocations).
      > I hope I've explained myself better this time. Thanks for the above[/color]
      particularly because I haven't written the funtion to delete an album yet.
      I'm sure this will do the job.
      Jimbo[color=blue]
      > I haven't tested these, by the way.
      >
      > Good luck,
      > Mike
      >
      > --
      > Michael Winter
      > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


      Comment

      • Michael Winter

        #4
        Re: removing array item

        On Tue, 31 Aug 2004 09:02:32 +0200, J. J. Cale <photom@netvisi on.net.il>
        wrote:
        [color=blue]
        > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
        > news:opsdjinqrx x13kvk@atlantis ...
        >[color=green]
        >> On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale
        >> <photom@netvisi on.net.il> wrote:[/color][/color]

        [snip]
        [color=blue][color=green][color=darkred]
        >>> event.srcElemen t[/color]
        >>
        >> Is this for the Web, or just an IE-only environment?[/color]
        >
        > Ideally it would be generic although currently I'm writing an HTA.[/color]

        The reason I asked is that event.srcElemen t is IE-only. You'll also have
        to check event.target, and possibly other properties, for the code to work
        with other browsers. However, since you're writing a HTA, it isn't of much
        concern at the moment.

        [snip]
        [color=blue]
        > the array albumArr contains an array of Album objects. I need to
        > delete the path for one image.[/color]

        Sorry.
        [color=blue]
        > the reference obj (here 'o') has the album name and the index in the
        > paths array pinned as attributes
        > I'm searching therefore for o.alb (the album name) and o.picNum the
        > offset into the paths array of the Album object.
        > while(i<n && albumArr[i].name != o.alb) {++i} should work
        > to find the album[/color]

        Yes, it would.

        [snip]
        [color=blue]
        > This would delete the entire album. I need to delete the image from the
        > paths array of the album. The slice method with a regex is what I'm
        > looking for but I'm just getting into regex.[/color]

        I don't think that a regular expression would be terribly useful. Of
        course, I'm not seeing the whole picture, so I could be wrong.

        [snip]

        function Album(albumName ) {
        this.name = albumName;
        this.paths = [];
        }
        Album.prototype .removePath = function(i) {
        for(var n = this.paths - 1; i < n; ++i) {
        this.paths[i] = this.paths[i + 1];
        }
        this.paths.leng th = n;
        };

        function removeArrSrc(o) {
        for(var i = 0, n = albumArr.length ; i < n; ++i) {
        if(albumArr[i].name == o.alb) {
        albumArr[i].removePath(o.p icNum);
        break;
        }
        }
        }

        I forsee a possible problem here. Say that you delete the first path
        (picNum 0). In the paths array, all of the elements will shift making the
        second path first, third path second, etc. If you then delete the second
        path (picNum 1), you'll actually be deleting the third path from the
        original list. Do you account for that?

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: removing array item

          J. J. Cale wrote:
          [color=blue]
          > I have the following. There must be something less cumbersome without push
          > pop. The function parameter obj is the event.srcElemen t[/color]

          which is IE only, use

          function foo(e)
          {
          if (!e)
          e = event;

          if (e)
          {
          var t = e.target ? e.target : e.srcElement;
          if (t)
          {
          // do something with t
          }
          }
          }

          <... on...="foo(even t);" ...>...</...>

          instead.
          [color=blue]
          > and has the attributes(prop erties?) picNum and alb pinned to it. The
          > function is part of a process to delete the selected obj, a photo in
          > this case from the album vis albumArr. TIA
          > Jimbo
          > function Album(albumName ) {
          > this.name=album Name;
          > this.paths=new Array();
          > }
          > var albumArr(); // holds an array of Albums();[/color]

          Invalid syntax. () is the call operator and can only be applied to
          functions/methods.

          var albumArr;

          Watch the JavaScript Console or the status bar of the browser window!
          [color=blue]
          > function removeArrSrc(ob j) {
          > var dummy=new Array();
          > for(i=0;i<album Arr.length;i++) {[/color]

          Note that `i' is declared global here, causing mostly undesired
          side effects. Use the `var' keyword (and optimize a bit):

          for (var i = 0, len = albumArr.length ; i < len; i++)
          {
          // ...
          }

          Since order does not seem to matter here, this can be
          further optimized:

          for (var i = albumArr.length ; i--; 0)
          {
          // ...
          }

          This loops from the last element to the first.
          [color=blue]
          > dummy[i]=new Album(albumArr[i].name);
          > [...][/color]

          To remove an array element, use the "delete" operator:

          delete albumAr[42];

          The array size will not be reduced, but further references
          to albumAr[42] will return `undefined' instead of an Album
          object reference which you can detect in your loop:

          for (var i = albumAr.length; i--; 0)
          {
          if (albumAr[i])
          {
          delete albumAr[i];
          }
          }

          More sophisticated but requires JavaScript 1.1 and compatibles:

          for (var i = albumAr.length; i--; 0)
          {
          if (typeof albumAr[i] != "undefined" )
          {
          delete albumAr[i];
          }
          }

          or

          for (var i = albumAr.length; i--; 0)
          {
          if (albumAr[i] && albumAr[i].constructor == Album)
          {
          delete albumAr[i];
          }
          }

          Even more sophisticated but requires an ECMAScript 3 or later
          implementation (e.g. JavaScript 1.5, JScript 5.6):

          for (var i = albumAr.length; i--; 0)
          {
          if (albumAr[i] instanceof Album)
          {
          delete albumAr[i];
          }
          }

          You may want to add further conditions which must be matched
          before the element will be deleted: your search parameters.


          HTH

          PointedEars

          P.S.
          Falsifying header information is not appropriate behavior (violating
          several Usenet/Internet standards, the Netiquette and most certainly
          the Acceptable Use Policy of service providers). It only *supports*
          spammers' doing: <http://www.interhack.n et/pubs/munging-harmful/>
          --
          If they outlaw encryption, only outlaws will
          62Xq3mrg45x69A9 6yvxi70gXrSdD+S Cw

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: removing array item

            Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:

            A few nitpicks:
            [color=blue]
            > function foo(e)
            > {
            > if (!e)
            > e = event;[/color]

            these two lines are not necessary if you do:
            [color=blue]
            > <... on...="foo(even t);" ...>...</...>[/color]

            The event will be the argument to the function in all browsers (since
            you pass it explicitly, and they all agree that "event" refers to the
            event in an intrinsic event handler). It is needed (in IE) only when
            you assign the function directly, as in:
            something.onwha tever = foo;
            [color=blue]
            > Even more sophisticated but requires an ECMAScript 3 or later
            > implementation (e.g. JavaScript 1.5, JScript 5.6):
            >
            > for (var i = albumAr.length; i--; 0)[/color]

            This would start "i" out at albumAr.length, which is one past
            the last element of the array. It should probably be
            ...(var i = albumAr.length - 1; ...

            The "0" can also be omitted (all three expressions in a "for"
            construct are optional).


            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: removing array item

              Lasse Reichstein Nielsen wrote:
              [color=blue]
              > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
              >> function foo(e)
              >> {
              >> if (!e)
              >> e = event;[/color]
              >
              > these two lines are not necessary if you do:
              >[color=green]
              >> <... on...="foo(even t);" ...>...</...>[/color]
              >
              > [...] It is needed (in IE) only when
              > you assign the function directly, as in:
              > something.onwha tever = foo;[/color]

              Correct, I confused both.
              [color=blue][color=green]
              >> Even more sophisticated but requires an ECMAScript 3 or later
              >> implementation (e.g. JavaScript 1.5, JScript 5.6):
              >>
              >> for (var i = albumAr.length; i--; 0)[/color]
              >
              > This would start "i" out at albumAr.length, which is one past
              > the last element of the array. It should probably be
              > ...(var i = albumAr.length - 1; ...[/color]

              No, it should not. The value of i is decreased the first time
              the loop is executed.
              [color=blue]
              > The "0" can also be omitted (all three expressions in a "for"
              > construct are optional).[/color]

              It cannot be omitted, the expressions are not optional in all
              implementations .


              PointedEars
              --
              Talk to the hand, the hand doesn't listen...

              Comment

              • Richard Cornford

                #8
                Re: removing array item

                Lasse Reichstein Nielsen wrote:[color=blue]
                > Thomas 'PointedEars' Lahn writes:[color=green]
                >> for (var i = albumAr.length; i--; 0)[/color]
                >
                > This would start "i" out at albumAr.length, which is one past
                > the last element of the array. It should probably be
                > ...(var i = albumAr.length - 1; ...[/color]
                <snip>

                No, the first two expressions are fine. The - i-- - post decrements the
                counter before its first use (assuming length was not zero, else there
                would be no array access) leaving it the equivalent of length-1 when it
                is first used.

                Rihcard.



                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: removing array item

                  Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                  [color=blue]
                  > No, it should not. The value of i is decreased the first time
                  > the loop is executed.[/color]

                  Ach, yes. The decrement is in the test, which is (apparently)
                  confuzing enough that I won't recommend it (for anything I have
                  to read :)
                  [color=blue]
                  > It cannot be omitted, the expressions are not optional in all
                  > implementations .[/color]

                  Can you name one where it isn't?

                  All three expressions are optional in Javascript 1.0 (tested in
                  Netscape 2.02) and in ECMAScript v1.

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Randy Webb

                    #10
                    Re: removing array item

                    Thomas 'PointedEars' Lahn wrote:
                    [color=blue]
                    > Lasse Reichstein Nielsen wrote:[/color]

                    <--snip-->
                    [color=blue]
                    >[color=green]
                    >>The "0" can also be omitted (all three expressions in a "for"
                    >>construct are optional).[/color]
                    >
                    >
                    > It cannot be omitted, the expressions are not optional in all
                    > implementations .[/color]

                    Can you quote one that does not allow them to be optional?


                    --
                    Randy
                    comp.lang.javas cript FAQ - http://jibbering.com/faq

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: removing array item

                      Lasse Reichstein Nielsen wrote:
                      [color=blue]
                      > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
                      >> It cannot be omitted, the expressions are not optional in all
                      >> implementations .[/color]
                      >
                      > Can you name one where it isn't?[/color]

                      Micro$oft JScript, according to the MSDN Library, which IIRC matches
                      reality in IE 6 SP-1 on Win2k SP-4.
                      [color=blue]
                      > All three expressions are optional in Javascript 1.0 (tested in
                      > Netscape 2.02) and in ECMAScript v1.[/color]

                      Yes, but non sequitur.


                      PointedEars
                      --
                      Bugzilla: It's not just for people with `e's in their names, anymore!

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: removing array item

                        J. J. Cale wrote:
                        [color=blue]
                        > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                        > news:opsdjinqrx x13kvk@atlantis ...[/color]

                        Please do not write attribution novels and trim your quotes.
                        <http://netmeister.org/news/learn2quote.htm l>
                        [color=blue][color=green]
                        >> I don't know what that's supposed to be, but I'm certain it'll cause an
                        >> error on most browsers. Perhaps you meant:
                        >>
                        >> var albumArr = [];
                        >>[/color]
                        > Sorry! It is declared var albumArr=new Array();[/color]

                        Both statements are equivalent, while the former (array literal) requires
                        AFAIK JavaScript 1.3 (supported from Netscape Navigator 4.0 on) and
                        compatibles, and later. If standards compliant, the implementation must
                        be standards compliant to ECMAScript 3.


                        Your Reply-To header still does not comply with Internet/Usenet standards.
                        nowayjimbo@nowa y012.net.il. is not an e-mail address!


                        PointedEars
                        --
                        squared over see squared

                        Comment

                        • Lasse Reichstein Nielsen

                          #13
                          Re: removing array item

                          Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                          [color=blue]
                          > Micro$oft JScript, according to the MSDN Library, which IIRC matches
                          > reality in IE 6 SP-1 on Win2k SP-4.[/color]

                          I don't have SP-1, but in IE 6 SP-2 (version
                          "6.0.2900.2180. xpsp_sp2_rtm.04 0803-2158") on WinXP, this script alerts
                          "10" as it should:
                          ---
                          var x = 4;
                          var y = 0;
                          for(;;){y+=x--;if (!x) {break;}}
                          alert(y);
                          ---
                          It seems to work as far back as IE 4 (I can't get IE 3 to run scripts)

                          The MSDN library is not a complete reference. There are features that
                          are not listed, and listed features that have unlisted exceptions.

                          I would be very surprised if there are any Javascript/ECMAScript/JScript-
                          implementations that doesn't allow you to omit all three expressions
                          of a "for", as that has been the expected behavior of the "for" construct
                          in all languages using a "C"-language-like "for" construct.

                          /L
                          --
                          Lasse Reichstein Nielsen - lrn@hotpop.com
                          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                          'Faith without judgement merely degrades the spirit divine.'

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: removing array item

                            Lasse Reichstein Nielsen wrote:
                            [color=blue]
                            > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
                            >> Micro$oft JScript, according to the MSDN Library, which IIRC matches
                            >> reality in IE 6 SP-1 on Win2k SP-4.[/color]
                            >
                            > I don't have SP-1, but in IE 6 SP-2 (version
                            > "6.0.2900.2180. xpsp_sp2_rtm.04 0803-2158") on WinXP, this script alerts
                            > "10" as it should:
                            > ---
                            > var x = 4;
                            > var y = 0;
                            > for(;;){y+=x--;if (!x) {break;}}
                            > alert(y);
                            > ---
                            > It seems to work as far back as IE 4 (I can't get IE 3 to run scripts)[/color]

                            Apparently, my mistake was to omit the trailing ";" in

                            for (var x = a.length; x--;)

                            Reincluding it, my example code also works in my IE (see above),
                            and yours works, of course.
                            [color=blue]
                            > The MSDN library is not a complete reference. There are features that
                            > are not listed, and listed features that have unlisted exceptions.[/color]

                            Unfortunately, yes.
                            [color=blue]
                            > I would be very surprised if there are any Javascript/ECMAScript/JScript-
                            > implementations that doesn't allow you to omit all three expressions
                            > of a "for", as that has been the expected behavior of the "for" construct
                            > in all languages using a "C"-language-like "for" construct.[/color]

                            Me too. According to the Client-side JavaScript 1.3 Reference,
                            they were already optional in JavaScript 1.0 (1995) and in the
                            first edition of ECMAScript (1997).


                            PointedEars
                            --
                            Minds are like parachutes, they only work when open.

                            Comment

                            Working...