Removing an element of an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • effendi@epitome.com.sg

    Removing an element of an array

    I wrote a simple script to remove an element of an array but I don't
    think this is the best way to go about it.

    I have a list of about five elements seperated by ";"

    I split the array using array.split(";" ) command and proceeded to
    update the elemment by assigning the null value to the arrayindex

    array[index]=""

    This of course assigns null to the element

    But there are two problems

    1. The array size is still five instead of 4
    and my list is now seperated by "," with an exta "," to go.

    Can anyone suggest a more effective way of doing this?

    Thank you.

  • Michael Winter

    #2
    Re: Removing an element of an array

    effendi@epitome .com.sg wrote:

    [snip]
    [color=blue]
    > array[index]=""
    >
    > This of course assigns null to the element[/color]

    It does not. It assigns an empty string to the element. This neither
    the same thing, nor does it delete the array element.

    To actually delete an array element, you must shift all elements above
    the deletion point to overwrite the to-be-deleted elements and then
    truncate the array. The splice method does this:

    Array.prototype .splice(index, count, item1, item2, ...);

    index - The position in the array where deletion/insertion begins.
    count - The number of elements to delete.
    item1 - An optional argument which specifies a value to insert once
    count elements have been deleted.

    For example, consider the following actions with array [1, 2, 3, 4]:

    result = array.splice(2, 1);

    array: [1, 2, 4]
    result: [3]


    result = array.splice(0, 2);

    array: [3, 4]
    result: [1, 2]


    result = array.splice(3, 1, 5, 6, 7);

    array: [1, 2, 3, 5, 6, 7]
    result: [4]


    result = array.splice(0, 4, 5);

    array: [5]
    result: [1, 2, 3, 4]

    From the penultimate examples, you can see that the splice method can
    expand an array if the number of inserted elements outnumbers the
    number of deleted elements.

    The only problem with the splice method is that IE provided a late
    implementation of it, meaning versions earlier than IE5.5 will not
    have the method (unless a newer JScript version has been installed).

    A complete implementation of splice is included below which can
    supplement an older browser:

    if('function' != typeof Array.prototype .splice) {
    Array.prototype .splice = function(s, dC) {s = +s || 0;
    var a = [],
    n = this.length,
    nI = Math.min(argume nts.length - 2, 0),
    i, j;
    s = (0 > s) ? Math.max(s + n, 0) : Math.min(s, n);
    dC = Math.min(Math.m ax(+dC || 0, 0), n - s);
    for(i = 0; i < dC; ++i) {a[i] = this[s + i];}
    if(nI < dC) {
    for(i = s, j = n - dC; i < j; ++i) {
    this[i + nI] = this[i + dC];
    }
    } else if(nI > dC) {
    for(i = n - 1, j = s + dC; i >= j; --i) {
    this[i + nI - dC] = this[i];
    }
    }
    for(i = s, j = 2; j < nI; ++i, ++j) {this[i] = arguments[j];}
    this.length = n - dC + nI;
    return a;
    };
    }

    You can just include the code above: if a browser doesn't implement
    splice, this code will automatically be used. If a browser does
    implement splice, the existing code will be used instead.

    If you're only interested in deleting elements from an array, the code
    above can be simplified slightly (but maintaining the same semantics) to:

    if('function' != typeof Array.prototype .splice) {
    Array.prototype .splice = function(s, dC) {s = +s || 0;
    var a = [],
    n = this.length,
    i, j;
    s = (0 > s) ? Math.max(s + n, 0) : Math.min(s, n);
    dC = Math.min(Math.m ax(+dC || 0, 0), n - s);
    for(i = 0; i < dC; ++i) {
    a[i] = this[s + i];
    this[s + i] = this[s + i + dC];
    }
    this.length = n - dC;
    return a;
    };
    }

    Finally, a /really/ bare-bones deletion routine would be:

    if('function' != typeof Array.prototype .splice) {
    Array.prototype .splice = function(s, dC) {
    for(var i = 0, n = this.length; i < dC; ++i) {
    this[s + i] = this[s + i + dC];
    }
    this.length = n - dC;
    };
    }

    [snip]

    Hope that helps,
    Mike

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

    Comment

    • Dietmar Meier

      #3
      Re: Removing an element of an array

      Michael Winter wrote:
      [color=blue]
      > A complete implementation of splice is included below which can
      > supplement an older browser: [...][/color]

      Tell me if I'm wrong, but I think this can be done easier:

      if (typeof Array == "function" && typeof Array.prototype .splice != "function") {
      Array.prototype .splice = mySplice;
      }

      function mySplice(p, n) {
      var i, j, r = new Array();
      if (!isNaN(p) && p>-1 && !isNaN(n) && n>0 && p<this.length) {
      for (j=0; j<n; j++) {
      r[r.length] = this[p+j];
      this[p+j] = this[p+j+n];
      }
      this.length -= n;
      }
      for (i=2; i<arguments.len gth; i++) {
      this[this.length] = arguments[i];
      }
      return r;
      }

      Did I forget anything?

      ciao, dhgm

      Comment

      • Effendi Baba

        #4
        Re: Removing an element of an array


        Thanks, I will implement this and try to absorb the "code' to supplement
        older browsers.




        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Michael Winter

          #5
          Re: Removing an element of an array

          Dietmar Meier wrote:
          [color=blue]
          > Michael Winter wrote:
          >[color=green]
          >> A complete implementation of splice is included below which can
          >> supplement an older browser: [...][/color]
          >
          > Tell me if I'm wrong, but I think this can be done easier:[/color]

          Possibly. It was difficult to see past the algorithm description in
          the specification to see precisely what was supposed to be
          accomplished. At least initially. I was happy enough with what I
          eventually produced that I couldn't really be bothered to take it any
          further. Anyone is welcome to suggest improvements, but the overall
          behaviour of the code cannot change (well, unless you're correcting a
          bug :D).

          The implementation doesn't quite conform to the specification;
          floating-point values for the first two arguments won't be truncated
          to integers. Replacing

          +? || 0

          with

          Math.floor(+? || 0)

          would cure that, where ? is s and dC.
          [color=blue]
          > if (typeof Array == "function" && typeof Array.prototype .splice !=
          > "function") {[/color]

          I don't think testing the Array constructor would be necessary, really.
          [color=blue]
          > Array.prototype .splice = mySplice;[/color]

          A function expression would be better. There's no need to introduce a
          global identifier when that identifier shouldn't ever be used by
          client code.
          [color=blue]
          > function mySplice(p, n) {[/color]

          The main problem with this function is that it doesn't follow the
          semantics laid out by the specification, therefore it doesn't serve as
          a general replacement. That's why I posted two other versions which
          are more specific to the OP's task.
          [color=blue]
          > var i, j, r = new Array();
          > if (!isNaN(p) && p>-1 && !isNaN(n) && n>0 && p<this.length) {[/color]

          The first few statements in my code make the start and deleteCount
          arguments (s and dC, respectively) take sensible values.

          First of all, both values are converted to numbers (though they should
          be converted to integers, as I mentioned above). If the conversion
          leads to NaN, the values are replaced by zero (0). Note that zero is a
          valid value for either argument as the splice function can also insert
          array elements (so passing zero might be intentional).

          If the starting value is negative, it is used to count back from the
          end of the array. For example, a value of -1 means "start at the last
          element". The starting value is limited to the range [0,length].

          The delete count is also limited: it may not be less than zero, nor
          can it extend past the end of the array.

          I'm afraid that the statement above doesn't allow for any of this.
          Neither can the starting value be negative, nor can the delete count
          be zero.

          [snip]
          [color=blue]
          > for (i=2; i<arguments.len gth; i++) {
          > this[this.length] = arguments[i];
          > }[/color]

          I'm afraid this isn't right either. The extra arguments to the
          function should be inserted at the starting point. They aren't
          unconditionally appended to the array.

          [snip]

          By all means try again. As I wrote earlier, any improvements (to /any/
          code I post) are more than welcome.

          Mike

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

          Comment

          • Michael Winter

            #6
            [Correction] Re: Removing an element of an array

            Michael Winter wrote:

            [snip]
            [color=blue]
            > nI = Math.min(argume nts.length - 2, 0),[/color]

            That should be a call to the Math.max method.

            [snip]

            Mike

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

            Comment

            Working...