Remove specified value from array

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

    Remove specified value from array

    How do I remove an item with a specified value from an array?

    i.e. array values 1,2,2,5,7,12,15 ,21

    remove 2 from array would return
    1,5,7,12,15,21

    (12 and 21 are NOT removed, duplicates are also removed)

    So far I have (val is value, ar is array, returns new array):

    function removeFromArray (val, ar){
    s = String(ar)
    // remove if not first item (global search)
    reRemove = new RegExp(","+val, "g")
    s = s.replace(reRem ove,"")
    // remove if at start of array
    reRemove = new RegExp("^"+val+ ",")
    s = s.replace(reRem ove,"")
    // remove if only item
    reRemove = new RegExp("^"+val+ "$")
    s = s.replace(reRem ove,"")
    return new Array(s)
    }

    However this seems to return 1,5,7,12,151 (the 151 is a result of the
    ',2' being removed from the '15,21' part of the array)

    How would I modify this to get it to work properly (maybe as one
    regular expression - broken down and explained though)?

    Also I want it to work with strings as well (possibly containing
    commas)

    i.e.

    array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
    remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"
  • Grant Wagner

    #2
    Re: Remove specified value from array

    Sam Collett wrote:
    [color=blue]
    > How do I remove an item with a specified value from an array?
    >
    > i.e. array values 1,2,2,5,7,12,15 ,21
    >
    > remove 2 from array would return
    > 1,5,7,12,15,21
    >
    > (12 and 21 are NOT removed, duplicates are also removed)
    >
    > So far I have (val is value, ar is array, returns new array):
    >
    > function removeFromArray (val, ar){
    > s = String(ar)
    > // remove if not first item (global search)
    > reRemove = new RegExp(","+val, "g")
    > s = s.replace(reRem ove,"")
    > // remove if at start of array
    > reRemove = new RegExp("^"+val+ ",")
    > s = s.replace(reRem ove,"")
    > // remove if only item
    > reRemove = new RegExp("^"+val+ "$")
    > s = s.replace(reRem ove,"")
    > return new Array(s)
    > }
    >
    > However this seems to return 1,5,7,12,151 (the 151 is a result of the
    > ',2' being removed from the '15,21' part of the array)
    >
    > How would I modify this to get it to work properly (maybe as one
    > regular expression - broken down and explained though)?
    >
    > Also I want it to work with strings as well (possibly containing
    > commas)
    >
    > i.e.
    >
    > array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
    > remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"[/color]

    First you say you want to remove values from an array, then you don't use
    a JavaScript Array object at all. That doesn't make much sense to me. If
    you want to remove items from an array, then remove items from an array:

    <script type="text/javascript">
    var theArray = [ 1,2,2,5,7,12,15 ,21 ];

    var theNewArray = removeItems(the Array, 2);
    alert(theNewArr ay.join(", "));

    function removeItems(arr ay, item) {
    var i = 0;
    while (i < array.length) {
    if (array[i] == item) {
    array.splice(i, 1);
    } else {
    i++;
    }
    }
    return array;
    }
    </script>

    But, you say to me, "I don't have an array like that". No problem, say
    you have the items in a comma-delimited string as original shown:

    var theValues = "1,2,2,5,7,12,1 5,21";
    var theArray = theValues.split (/,/);

    Doing it for names would work just as well:

    var theNewArray = (theNamesArray, "Doe, John");

    The only problem would be turning your comma-delimited list of names into
    an array. "'Doe, John', 'Smith, Joe'" couldn't be turned into an array by
    ..split(/,/). There are a number of solutions to this, however the easiest
    would be to use another delimiter (one that is unlikely to appear as part
    of a person's name in the original string):

    var theNames = "'Doe, John'@#!'Smith, Joe'@#!'Jones, Mary'";
    var theNamesArray = theNames.split(/@#!/);

    Better still, store the first and last names as two properties of an
    object, then store that object in the array.

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Douglas Crockford

      #3
      Re: Remove specified value from array

      > How do I remove an item with a specified value from an array?[color=blue]
      >
      > i.e. array values 1,2,2,5,7,12,15 ,21
      >
      > remove 2 from array would return
      > 1,5,7,12,15,21
      >
      > (12 and 21 are NOT removed, duplicates are also removed)
      >
      > So far I have (val is value, ar is array, returns new array):
      >
      > function removeFromArray (val, ar){
      > s = String(ar)
      > // remove if not first item (global search)
      > reRemove = new RegExp(","+val, "g")
      > s = s.replace(reRem ove,"")
      > // remove if at start of array
      > reRemove = new RegExp("^"+val+ ",")
      > s = s.replace(reRem ove,"")
      > // remove if only item
      > reRemove = new RegExp("^"+val+ "$")
      > s = s.replace(reRem ove,"")
      > return new Array(s)
      > }
      >
      > However this seems to return 1,5,7,12,151 (the 151 is a result of the
      > ',2' being removed from the '15,21' part of the array)
      >
      > How would I modify this to get it to work properly (maybe as one
      > regular expression - broken down and explained though)?
      >
      > Also I want it to work with strings as well (possibly containing
      > commas)
      >
      > i.e.
      >
      > array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
      > remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"[/color]

      Mother of pearl! Arrays are useful data structures. You ought not to be
      transforming out of array space into string space and doing RegExp operations.
      That is wildly inefficient and error prone. Stay in array space.

      If you know the subscript of the element to delete, you can use the splice()
      method to extract it. So,

      var my_array = [1,2,2,5,7,12,15 ,21];
      my_array.splice (1, 2);

      my_array now contains [1,5,7,12,15,21];

      Similarly,

      my_array = ["Bloggs, Jo", "Doe, John", "Doe, Jane"];
      my_array.splice (2, 1);

      my_array now contains ["Bloggs, Jo", "Doe, John"].

      You can find the subscripts of the elements with a for loop.





      Comment

      • Dr John Stockton

        #4
        Re: Remove specified value from array

        JRS: In article <2030665d.03072 10614.589eae3d@ posting.google. com>, seen
        in news:comp.lang. javascript, Sam Collett <sam_collett@ly cos.co.uk>
        posted at Mon, 21 Jul 2003 07:14:54 :-
        [color=blue]
        >How do I remove an item with a specified value from an array?[/color]

        The following is for removing undefined values from an array; note that
        u is undefined. Define u as being the value to be removed, and ISTM
        that it should work. Consider the ===, which can be == if u is defined
        (? depending on whether you think '2' == 2 ?).

        function scanReduce(A) { // After Dom Leonard, c.l.j, 2003-07-15
        var u, i=-1, j=-1, k=A.length
        while (++j < k) if (!(A[j]===u)) A[++i]=A[j]
        A.length = ++i
        return A }

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
        Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
        Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
        Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

        Comment

        • Dr John Stockton

          #5
          Re: Remove specified value from array

          JRS: In article <bfh9eg$2dd$1@s un-news.laserlink. net>, seen in
          news:comp.lang. javascript, Douglas Crockford <nospam@laserli nk.net>
          posted at Mon, 21 Jul 2003 10:56:26 :-
          [color=blue]
          >If you know the subscript of the element to delete, you can use the splice()
          >method to extract it. So,
          >
          > var my_array = [1,2,2,5,7,12,15 ,21];
          > my_array.splice (1, 2);[/color]

          There are two small problems with .splice() - it's not in older
          javascript, and it looks horribly like a typo for .slice() .

          It's a pity that the example uses 2 in the array; it would be better
          with all elements larger by say 50.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          • Grant Wagner

            #6
            Re: Remove specified value from array

            Actually, given your initial variables:

            var theArray = [ 1,2,2,5,7,12,15 ,21 ];
            var toRemove = [ 1,7 ]
            var theNewArray = removeItems(the Array, toRemove);

            The resulting array should be 2,2,5,12,15,21 not 1,5,12,15,21

            Anyway, use nested loops. Just repeat the original code for each value passed in:

            <script type="text/javascript">
            var theArray = [ 1,2,2,5,7,12,15 ,21 ];
            var toRemove = [ 1,7 ];
            var theNewArray = removeItems(the Array, toRemove);
            alert(theNewArr ay);

            function removeItems(ori ginalArray, itemsToRemove) {
            var j;
            for (var i = 0; i < itemsToRemove.l ength; i++) {
            j = 0;
            while (j < originalArray.l ength) {
            if (originalArray[j] == itemsToRemove[i]) {
            originalArray.s plice(j, 1);
            } else {
            j++;
            }
            }
            }
            return originalArray;
            }
            </script>

            Actually, I'd make removeItems() a method of the Array object:

            Array.prototype .removeItems = function(itemsT oRemove) {
            var j;
            for (var i = 0; i < itemsToRemove.l ength; i++) {
            j = 0;
            while (j < this.length) {
            if (this[j] == itemsToRemove[i]) {
            this.splice(j, 1);
            } else {
            j++;
            }
            }
            }
            }
            theArray.remove Items(toRemove) ;
            alert(theArray) ;

            Note, this only works for comparable data types (Number, String). If the contents of the array are a more
            complex object (such as another Array), then you'll need a method that allows you to evaluate their equality
            (== won't do it).

            I hope I get a good mark from your instructor for this.


            Sam Collett wrote:
            [color=blue]
            > I wasn't sure how to do it using the array object so thought that
            > using regular expressions would be the solution (i.e. converting the
            > array to a string and then back into an array after performing the
            > removal)
            >
            > Now that you go through it though it seems as though I didn't think of
            > the obvious (looping though the array and splicing it when the item
            > was found).
            >
            > The next step is to try to remove an array of items from the original
            > array.
            >
            > e.g.
            > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
            > var toRemove = [ 1,7 ]
            > var theNewArray = removeItems(the Array, toRemove);
            >
            > returns 1,5,12,15,21
            >
            > Would it be possible to do a check to see if the second parameter
            > (toRemove) is an array or a string, performing a loop on the toRemove
            > if it is an array?
            >
            > Maybe it would be simpler to do a nested loop in the function
            > (converting toRemove to an array, even if the supplied value is a
            > number or a string)?
            >
            > Thanks for the help
            >
            > Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1C21D6. CE3F5C7A@agrico reunited.com>.. .[color=green]
            > > Sam Collett wrote:
            > >[color=darkred]
            > > > How do I remove an item with a specified value from an array?
            > > >
            > > > i.e. array values 1,2,2,5,7,12,15 ,21
            > > >
            > > > remove 2 from array would return
            > > > 1,5,7,12,15,21
            > > >
            > > > (12 and 21 are NOT removed, duplicates are also removed)
            > > >
            > > > So far I have (val is value, ar is array, returns new array):
            > > >
            > > > function removeFromArray (val, ar){
            > > > s = String(ar)
            > > > // remove if not first item (global search)
            > > > reRemove = new RegExp(","+val, "g")
            > > > s = s.replace(reRem ove,"")
            > > > // remove if at start of array
            > > > reRemove = new RegExp("^"+val+ ",")
            > > > s = s.replace(reRem ove,"")
            > > > // remove if only item
            > > > reRemove = new RegExp("^"+val+ "$")
            > > > s = s.replace(reRem ove,"")
            > > > return new Array(s)
            > > > }
            > > >
            > > > However this seems to return 1,5,7,12,151 (the 151 is a result of the
            > > > ',2' being removed from the '15,21' part of the array)
            > > >
            > > > How would I modify this to get it to work properly (maybe as one
            > > > regular expression - broken down and explained though)?
            > > >
            > > > Also I want it to work with strings as well (possibly containing
            > > > commas)
            > > >
            > > > i.e.
            > > >
            > > > array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
            > > > remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"[/color]
            > >
            > > First you say you want to remove values from an array, then you don't use
            > > a JavaScript Array object at all. That doesn't make much sense to me. If
            > > you want to remove items from an array, then remove items from an array:
            > >
            > > <script type="text/javascript">
            > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
            > >
            > > var theNewArray = removeItems(the Array, 2);
            > > alert(theNewArr ay.join(", "));
            > >
            > > function removeItems(arr ay, item) {
            > > var i = 0;
            > > while (i < array.length) {
            > > if (array[i] == item) {
            > > array.splice(i, 1);
            > > } else {
            > > i++;
            > > }
            > > }
            > > return array;
            > > }
            > > </script>
            > >
            > > But, you say to me, "I don't have an array like that". No problem, say
            > > you have the items in a comma-delimited string as original shown:
            > >
            > > var theValues = "1,2,2,5,7,12,1 5,21";
            > > var theArray = theValues.split (/,/);
            > >
            > > Doing it for names would work just as well:
            > >
            > > var theNewArray = (theNamesArray, "Doe, John");
            > >
            > > The only problem would be turning your comma-delimited list of names into
            > > an array. "'Doe, John', 'Smith, Joe'" couldn't be turned into an array by
            > > .split(/,/). There are a number of solutions to this, however the easiest
            > > would be to use another delimiter (one that is unlikely to appear as part
            > > of a person's name in the original string):
            > >
            > > var theNames = "'Doe, John'@#!'Smith, Joe'@#!'Jones, Mary'";
            > > var theNamesArray = theNames.split(/@#!/);
            > >
            > > Better still, store the first and last names as two properties of an
            > > object, then store that object in the array.[/color][/color]

            --
            | Grant Wagner <gwagner@agrico reunited.com>

            * Client-side Javascript and Netscape 4 DOM Reference available at:
            * http://devedge.netscape.com/library/...ce/frames.html
            * Internet Explorer DOM Reference available at:
            * http://msdn.microsoft.com/workshop/a...ence_entry.asp
            * Netscape 6/7 DOM Reference available at:
            * http://www.mozilla.org/docs/dom/domref/
            * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
            * http://www.mozilla.org/docs/web-deve...upgrade_2.html


            Comment

            • Greg

              #7
              Re: Remove specified value from array

              sam_collett@lyc os.co.uk (Sam Collett) wrote in message news:<2030665d. 0307220129.301f 51aa@posting.go ogle.com>...[color=blue]
              > I wasn't sure how to do it using the array object so thought that
              > using regular expressions would be the solution (i.e. converting the
              > array to a string and then back into an array after performing the
              > removal)
              >
              > Now that you go through it though it seems as though I didn't think of
              > the obvious (looping though the array and splicing it when the item
              > was found).
              >
              > The next step is to try to remove an array of items from the original
              > array.
              >
              > e.g.
              > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
              > var toRemove = [ 1,7 ]
              > var theNewArray = removeItems(the Array, toRemove);
              >
              > returns 1,5,12,15,21
              >
              > Would it be possible to do a check to see if the second parameter
              > (toRemove) is an array or a string, performing a loop on the toRemove
              > if it is an array?
              >
              > Maybe it would be simpler to do a nested loop in the function
              > (converting toRemove to an array, even if the supplied value is a
              > number or a string)?
              >
              > Thanks for the help
              >
              > Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1C21D6. CE3F5C7A@agrico reunited.com>.. .[color=green]
              > > Sam Collett wrote:
              > >[color=darkred]
              > > > How do I remove an item with a specified value from an array?
              > > >
              > > > i.e. array values 1,2,2,5,7,12,15 ,21
              > > >
              > > > remove 2 from array would return
              > > > 1,5,7,12,15,21
              > > >
              > > > (12 and 21 are NOT removed, duplicates are also removed)
              > > >
              > > > So far I have (val is value, ar is array, returns new array):
              > > >
              > > > function removeFromArray (val, ar){
              > > > s = String(ar)
              > > > // remove if not first item (global search)
              > > > reRemove = new RegExp(","+val, "g")
              > > > s = s.replace(reRem ove,"")
              > > > // remove if at start of array
              > > > reRemove = new RegExp("^"+val+ ",")
              > > > s = s.replace(reRem ove,"")
              > > > // remove if only item
              > > > reRemove = new RegExp("^"+val+ "$")
              > > > s = s.replace(reRem ove,"")
              > > > return new Array(s)
              > > > }
              > > >
              > > > However this seems to return 1,5,7,12,151 (the 151 is a result of the
              > > > ',2' being removed from the '15,21' part of the array)
              > > >
              > > > How would I modify this to get it to work properly (maybe as one
              > > > regular expression - broken down and explained though)?
              > > >
              > > > Also I want it to work with strings as well (possibly containing
              > > > commas)
              > > >
              > > > i.e.
              > > >
              > > > array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
              > > > remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"[/color]
              > >
              > > First you say you want to remove values from an array, then you don't use
              > > a JavaScript Array object at all. That doesn't make much sense to me. If
              > > you want to remove items from an array, then remove items from an array:
              > >
              > > <script type="text/javascript">
              > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
              > >
              > > var theNewArray = removeItems(the Array, 2);
              > > alert(theNewArr ay.join(", "));
              > >
              > > function removeItems(arr ay, item) {
              > > var i = 0;
              > > while (i < array.length) {
              > > if (array[i] == item) {
              > > array.splice(i, 1);
              > > } else {
              > > i++;
              > > }
              > > }
              > > return array;
              > > }
              > > </script>
              > >
              > > But, you say to me, "I don't have an array like that". No problem, say
              > > you have the items in a comma-delimited string as original shown:
              > >
              > > var theValues = "1,2,2,5,7,12,1 5,21";
              > > var theArray = theValues.split (/,/);
              > >
              > > Doing it for names would work just as well:
              > >
              > > var theNewArray = (theNamesArray, "Doe, John");
              > >
              > > The only problem would be turning your comma-delimited list of names into
              > > an array. "'Doe, John', 'Smith, Joe'" couldn't be turned into an array by
              > > .split(/,/). There are a number of solutions to this, however the easiest
              > > would be to use another delimiter (one that is unlikely to appear as part
              > > of a person's name in the original string):
              > >
              > > var theNames = "'Doe, John'@#!'Smith, Joe'@#!'Jones, Mary'";
              > > var theNamesArray = theNames.split(/@#!/);
              > >
              > > Better still, store the first and last names as two properties of an
              > > object, then store that object in the array.
              > >
              > > --
              > > | Grant Wagner <gwagner@agrico reunited.com>
              > >
              > > * Client-side Javascript and Netscape 4 DOM Reference available at:
              > > *
              > > http://devedge.netscape.com/library/...ce/frames.html
              > >
              > > * Internet Explorer DOM Reference available at:
              > > *
              > > http://msdn.microsoft.com/workshop/a...ence_entry.asp
              > >
              > > * Netscape 6/7 DOM Reference available at:
              > > * http://www.mozilla.org/docs/dom/domref/
              > > * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
              > > * http://www.mozilla.org/docs/web-deve...upgrade_2.html[/color][/color]

              I'm no expert, but I think that your regex idea would work with a
              little modification. Here's an example that seems to work:

              <script type='text/javascript'>
              function removeVal(arr, valToRemove){
              // Normalize to a string like !val!!val!!val!
              var s = '!' + arr.join('!!') + '!';
              // Remove targeted values with delimiters
              s = s.replace(new RegExp('!' + valToRemove + '!', 'g'), '');
              // Remove delimiter added to end in step 1
              s = s.replace(/^!/, '');
              // Remove delimiter added to start in step 1
              s = s.replace(/!$/, '');
              // Convert to array
              return s.split('!!');
              }

              function test(){
              var arr = [2,1,2,2,5,7,12, 15,21,2]; // Check case with end vals
              var s = arr.toString();
              arr = removeVal(arr, 2)
              alert(s + '\n' + arr.toString()) ;
              }
              </script>

              <button onclick='test() ' />test()</button>

              Comment

              • Sam Collett

                #8
                Re: Remove specified value from array

                Thanks for that. Don't know how I got the wrong resulting array
                (should have double checked before I posted).

                This is actually for an ASP page I was doing, but I use javascript
                server-side (instead of VBScript) as it is easily migrated over to
                client-side.

                Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1D8066. 2978531E@agrico reunited.com>.. .[color=blue]
                > Actually, given your initial variables:
                >
                > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                > var toRemove = [ 1,7 ]
                > var theNewArray = removeItems(the Array, toRemove);
                >
                > The resulting array should be 2,2,5,12,15,21 not 1,5,12,15,21
                >
                > Anyway, use nested loops. Just repeat the original code for each value passed in:
                >
                > <script type="text/javascript">
                > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                > var toRemove = [ 1,7 ];
                > var theNewArray = removeItems(the Array, toRemove);
                > alert(theNewArr ay);
                >
                > function removeItems(ori ginalArray, itemsToRemove) {
                > var j;
                > for (var i = 0; i < itemsToRemove.l ength; i++) {
                > j = 0;
                > while (j < originalArray.l ength) {
                > if (originalArray[j] == itemsToRemove[i]) {
                > originalArray.s plice(j, 1);
                > } else {
                > j++;
                > }
                > }
                > }
                > return originalArray;
                > }
                > </script>
                >
                > Actually, I'd make removeItems() a method of the Array object:
                >
                > Array.prototype .removeItems = function(itemsT oRemove) {
                > var j;
                > for (var i = 0; i < itemsToRemove.l ength; i++) {
                > j = 0;
                > while (j < this.length) {
                > if (this[j] == itemsToRemove[i]) {
                > this.splice(j, 1);
                > } else {
                > j++;
                > }
                > }
                > }
                > }
                > theArray.remove Items(toRemove) ;
                > alert(theArray) ;
                >
                > Note, this only works for comparable data types (Number, String). If the contents of the array are a more
                > complex object (such as another Array), then you'll need a method that allows you to evaluate their equality
                > (== won't do it).
                >
                > I hope I get a good mark from your instructor for this.
                >
                >
                > Sam Collett wrote:
                >[color=green]
                > > I wasn't sure how to do it using the array object so thought that
                > > using regular expressions would be the solution (i.e. converting the
                > > array to a string and then back into an array after performing the
                > > removal)
                > >
                > > Now that you go through it though it seems as though I didn't think of
                > > the obvious (looping though the array and splicing it when the item
                > > was found).
                > >
                > > The next step is to try to remove an array of items from the original
                > > array.
                > >
                > > e.g.
                > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                > > var toRemove = [ 1,7 ]
                > > var theNewArray = removeItems(the Array, toRemove);
                > >
                > > returns 1,5,12,15,21
                > >
                > > Would it be possible to do a check to see if the second parameter
                > > (toRemove) is an array or a string, performing a loop on the toRemove
                > > if it is an array?
                > >
                > > Maybe it would be simpler to do a nested loop in the function
                > > (converting toRemove to an array, even if the supplied value is a
                > > number or a string)?
                > >
                > > Thanks for the help
                > >
                > > Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1C21D6. CE3F5C7A@agrico reunited.com>.. .[color=darkred]
                > > > Sam Collett wrote:
                > > >
                > > > > How do I remove an item with a specified value from an array?
                > > > >
                > > > > i.e. array values 1,2,2,5,7,12,15 ,21
                > > > >
                > > > > remove 2 from array would return
                > > > > 1,5,7,12,15,21
                > > > >
                > > > > (12 and 21 are NOT removed, duplicates are also removed)
                > > > >
                > > > > So far I have (val is value, ar is array, returns new array):
                > > > >
                > > > > function removeFromArray (val, ar){
                > > > > s = String(ar)
                > > > > // remove if not first item (global search)
                > > > > reRemove = new RegExp(","+val, "g")
                > > > > s = s.replace(reRem ove,"")
                > > > > // remove if at start of array
                > > > > reRemove = new RegExp("^"+val+ ",")
                > > > > s = s.replace(reRem ove,"")
                > > > > // remove if only item
                > > > > reRemove = new RegExp("^"+val+ "$")
                > > > > s = s.replace(reRem ove,"")
                > > > > return new Array(s)
                > > > > }
                > > > >
                > > > > However this seems to return 1,5,7,12,151 (the 151 is a result of the
                > > > > ',2' being removed from the '15,21' part of the array)
                > > > >
                > > > > How would I modify this to get it to work properly (maybe as one
                > > > > regular expression - broken down and explained though)?
                > > > >
                > > > > Also I want it to work with strings as well (possibly containing
                > > > > commas)
                > > > >
                > > > > i.e.
                > > > >
                > > > > array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
                > > > > remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"
                > > >
                > > > First you say you want to remove values from an array, then you don't use
                > > > a JavaScript Array object at all. That doesn't make much sense to me. If
                > > > you want to remove items from an array, then remove items from an array:
                > > >
                > > > <script type="text/javascript">
                > > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                > > >
                > > > var theNewArray = removeItems(the Array, 2);
                > > > alert(theNewArr ay.join(", "));
                > > >
                > > > function removeItems(arr ay, item) {
                > > > var i = 0;
                > > > while (i < array.length) {
                > > > if (array[i] == item) {
                > > > array.splice(i, 1);
                > > > } else {
                > > > i++;
                > > > }
                > > > }
                > > > return array;
                > > > }
                > > > </script>
                > > >
                > > > But, you say to me, "I don't have an array like that". No problem, say
                > > > you have the items in a comma-delimited string as original shown:
                > > >
                > > > var theValues = "1,2,2,5,7,12,1 5,21";
                > > > var theArray = theValues.split (/,/);
                > > >
                > > > Doing it for names would work just as well:
                > > >
                > > > var theNewArray = (theNamesArray, "Doe, John");
                > > >
                > > > The only problem would be turning your comma-delimited list of names into
                > > > an array. "'Doe, John', 'Smith, Joe'" couldn't be turned into an array by
                > > > .split(/,/). There are a number of solutions to this, however the easiest
                > > > would be to use another delimiter (one that is unlikely to appear as part
                > > > of a person's name in the original string):
                > > >
                > > > var theNames = "'Doe, John'@#!'Smith, Joe'@#!'Jones, Mary'";
                > > > var theNamesArray = theNames.split(/@#!/);
                > > >
                > > > Better still, store the first and last names as two properties of an
                > > > object, then store that object in the array.[/color][/color]
                >
                > --
                > | Grant Wagner <gwagner@agrico reunited.com>
                >
                > * Client-side Javascript and Netscape 4 DOM Reference available at:
                > * http://devedge.netscape.com/library/...ce/frames.html
                > * Internet Explorer DOM Reference available at:
                > * http://msdn.microsoft.com/workshop/a...ence_entry.asp
                > * Netscape 6/7 DOM Reference available at:
                > * http://www.mozilla.org/docs/dom/domref/
                > * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
                > * http://www.mozilla.org/docs/web-deve...upgrade_2.html[/color]

                Comment

                • Sam Collett

                  #9
                  Re: Remove specified value from array

                  It only seems to remove the first item in the array (using the
                  prototype function), but using the removeItems function it performs as
                  expected.

                  e.g.

                  original array:[17,2,5,675]

                  remove array:[17,2]

                  resulting array:[2,5,675]

                  Only the first item (17) seems to be removed from the array.

                  Do both arrays need to be sorted, and if so how would I go about doing
                  that?

                  Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1D8066. 2978531E@agrico reunited.com>.. .[color=blue]
                  > Actually, given your initial variables:
                  >
                  > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                  > var toRemove = [ 1,7 ]
                  > var theNewArray = removeItems(the Array, toRemove);
                  >
                  > The resulting array should be 2,2,5,12,15,21 not 1,5,12,15,21
                  >
                  > Anyway, use nested loops. Just repeat the original code for each value passed in:
                  >
                  > <script type="text/javascript">
                  > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                  > var toRemove = [ 1,7 ];
                  > var theNewArray = removeItems(the Array, toRemove);
                  > alert(theNewArr ay);
                  >
                  > function removeItems(ori ginalArray, itemsToRemove) {
                  > var j;
                  > for (var i = 0; i < itemsToRemove.l ength; i++) {
                  > j = 0;
                  > while (j < originalArray.l ength) {
                  > if (originalArray[j] == itemsToRemove[i]) {
                  > originalArray.s plice(j, 1);
                  > } else {
                  > j++;
                  > }
                  > }
                  > }
                  > return originalArray;
                  > }
                  > </script>
                  >
                  > Actually, I'd make removeItems() a method of the Array object:
                  >
                  > Array.prototype .removeItems = function(itemsT oRemove) {
                  > var j;
                  > for (var i = 0; i < itemsToRemove.l ength; i++) {
                  > j = 0;
                  > while (j < this.length) {
                  > if (this[j] == itemsToRemove[i]) {
                  > this.splice(j, 1);
                  > } else {
                  > j++;
                  > }
                  > }
                  > }
                  > }
                  > theArray.remove Items(toRemove) ;
                  > alert(theArray) ;
                  >
                  > Note, this only works for comparable data types (Number, String). If the contents of the array are a more
                  > complex object (such as another Array), then you'll need a method that allows you to evaluate their equality
                  > (== won't do it).
                  >
                  > I hope I get a good mark from your instructor for this.
                  >
                  >
                  > Sam Collett wrote:
                  >[color=green]
                  > > I wasn't sure how to do it using the array object so thought that
                  > > using regular expressions would be the solution (i.e. converting the
                  > > array to a string and then back into an array after performing the
                  > > removal)
                  > >
                  > > Now that you go through it though it seems as though I didn't think of
                  > > the obvious (looping though the array and splicing it when the item
                  > > was found).
                  > >
                  > > The next step is to try to remove an array of items from the original
                  > > array.
                  > >
                  > > e.g.
                  > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                  > > var toRemove = [ 1,7 ]
                  > > var theNewArray = removeItems(the Array, toRemove);
                  > >
                  > > returns 1,5,12,15,21
                  > >
                  > > Would it be possible to do a check to see if the second parameter
                  > > (toRemove) is an array or a string, performing a loop on the toRemove
                  > > if it is an array?
                  > >
                  > > Maybe it would be simpler to do a nested loop in the function
                  > > (converting toRemove to an array, even if the supplied value is a
                  > > number or a string)?
                  > >
                  > > Thanks for the help
                  > >
                  > > Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1C21D6. CE3F5C7A@agrico reunited.com>.. .[color=darkred]
                  > > > Sam Collett wrote:
                  > > >
                  > > > > How do I remove an item with a specified value from an array?
                  > > > >
                  > > > > i.e. array values 1,2,2,5,7,12,15 ,21
                  > > > >
                  > > > > remove 2 from array would return
                  > > > > 1,5,7,12,15,21
                  > > > >
                  > > > > (12 and 21 are NOT removed, duplicates are also removed)
                  > > > >
                  > > > > So far I have (val is value, ar is array, returns new array):
                  > > > >
                  > > > > function removeFromArray (val, ar){
                  > > > > s = String(ar)
                  > > > > // remove if not first item (global search)
                  > > > > reRemove = new RegExp(","+val, "g")
                  > > > > s = s.replace(reRem ove,"")
                  > > > > // remove if at start of array
                  > > > > reRemove = new RegExp("^"+val+ ",")
                  > > > > s = s.replace(reRem ove,"")
                  > > > > // remove if only item
                  > > > > reRemove = new RegExp("^"+val+ "$")
                  > > > > s = s.replace(reRem ove,"")
                  > > > > return new Array(s)
                  > > > > }
                  > > > >
                  > > > > However this seems to return 1,5,7,12,151 (the 151 is a result of the
                  > > > > ',2' being removed from the '15,21' part of the array)
                  > > > >
                  > > > > How would I modify this to get it to work properly (maybe as one
                  > > > > regular expression - broken down and explained though)?
                  > > > >
                  > > > > Also I want it to work with strings as well (possibly containing
                  > > > > commas)
                  > > > >
                  > > > > i.e.
                  > > > >
                  > > > > array = "Bloggs, Jo", "Doe, John", "Doe, Jane"
                  > > > > remove "Doe, Jane" from array to return "Bloggs, Jo","Doe, John"
                  > > >
                  > > > First you say you want to remove values from an array, then you don't use
                  > > > a JavaScript Array object at all. That doesn't make much sense to me. If
                  > > > you want to remove items from an array, then remove items from an array:
                  > > >
                  > > > <script type="text/javascript">
                  > > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                  > > >
                  > > > var theNewArray = removeItems(the Array, 2);
                  > > > alert(theNewArr ay.join(", "));
                  > > >
                  > > > function removeItems(arr ay, item) {
                  > > > var i = 0;
                  > > > while (i < array.length) {
                  > > > if (array[i] == item) {
                  > > > array.splice(i, 1);
                  > > > } else {
                  > > > i++;
                  > > > }
                  > > > }
                  > > > return array;
                  > > > }
                  > > > </script>
                  > > >
                  > > > But, you say to me, "I don't have an array like that". No problem, say
                  > > > you have the items in a comma-delimited string as original shown:
                  > > >
                  > > > var theValues = "1,2,2,5,7,12,1 5,21";
                  > > > var theArray = theValues.split (/,/);
                  > > >
                  > > > Doing it for names would work just as well:
                  > > >
                  > > > var theNewArray = (theNamesArray, "Doe, John");
                  > > >
                  > > > The only problem would be turning your comma-delimited list of names into
                  > > > an array. "'Doe, John', 'Smith, Joe'" couldn't be turned into an array by
                  > > > .split(/,/). There are a number of solutions to this, however the easiest
                  > > > would be to use another delimiter (one that is unlikely to appear as part
                  > > > of a person's name in the original string):
                  > > >
                  > > > var theNames = "'Doe, John'@#!'Smith, Joe'@#!'Jones, Mary'";
                  > > > var theNamesArray = theNames.split(/@#!/);
                  > > >
                  > > > Better still, store the first and last names as two properties of an
                  > > > object, then store that object in the array.[/color][/color]
                  >
                  > --
                  > | Grant Wagner <gwagner@agrico reunited.com>
                  >
                  > * Client-side Javascript and Netscape 4 DOM Reference available at:
                  > * http://devedge.netscape.com/library/...ce/frames.html
                  > * Internet Explorer DOM Reference available at:
                  > * http://msdn.microsoft.com/workshop/a...ence_entry.asp
                  > * Netscape 6/7 DOM Reference available at:
                  > * http://www.mozilla.org/docs/dom/domref/
                  > * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
                  > * http://www.mozilla.org/docs/web-deve...upgrade_2.html[/color]

                  Comment

                  • Grant Wagner

                    #10
                    Re: Remove specified value from array

                    Don't know what to tell you, I ran the following code in IE6SP1, Mozilla 1.5b, Netscape 4.78 and Opera 7.11 and
                    they all generated [5, 675]:

                    var theArray = [17,2,5,675];
                    var toRemove = [17,2];
                    // var toRemove = 5;
                    Array.prototype .removeItems = function(itemsT oRemove) {

                    if (!/Array/.test(itemsToRe move.constructo r)) {
                    itemsToRemove = [ itemsToRemove ];
                    }

                    var j;
                    for (var i = 0; i < itemsToRemove.l ength; i++) {
                    j = 0;
                    while (j < this.length) {
                    if (this[j] == itemsToRemove[i]) {
                    this.splice(j, 1);
                    } else {
                    j++;
                    }
                    }
                    }
                    }
                    theArray.remove Items(toRemove) ;
                    alert(theArray) ;

                    There may be differences in the way Array.splice() acts in ASP and the way it's implemented in client-side
                    JavaScript on the tested browsers. The arrays shouldn't need to be in any particular order for the code to work.

                    Note I've also included some code requested by Greg that tests the removal parameter, so the method can now remove
                    a single value or an array. It would be trival to also add functionality to allow: Array.removeIte ms(17, 2); in
                    addition to Array.removeIte ms([17, 2]); and Array.removeIte ms(5);

                    Sam Collett wrote:
                    [color=blue]
                    > It only seems to remove the first item in the array (using the
                    > prototype function), but using the removeItems function it performs as
                    > expected.
                    >
                    > e.g.
                    >
                    > original array:[17,2,5,675]
                    >
                    > remove array:[17,2]
                    >
                    > resulting array:[2,5,675]
                    >
                    > Only the first item (17) seems to be removed from the array.
                    >
                    > Do both arrays need to be sorted, and if so how would I go about doing
                    > that?
                    >
                    > Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1D8066. 2978531E@agrico reunited.com>.. .[color=green]
                    > > Actually, given your initial variables:
                    > >
                    > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                    > > var toRemove = [ 1,7 ]
                    > > var theNewArray = removeItems(the Array, toRemove);
                    > >
                    > > The resulting array should be 2,2,5,12,15,21 not 1,5,12,15,21
                    > >
                    > > Anyway, use nested loops. Just repeat the original code for each value passed in:
                    > >
                    > > <script type="text/javascript">
                    > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                    > > var toRemove = [ 1,7 ];
                    > > var theNewArray = removeItems(the Array, toRemove);
                    > > alert(theNewArr ay);
                    > >
                    > > function removeItems(ori ginalArray, itemsToRemove) {
                    > > var j;
                    > > for (var i = 0; i < itemsToRemove.l ength; i++) {
                    > > j = 0;
                    > > while (j < originalArray.l ength) {
                    > > if (originalArray[j] == itemsToRemove[i]) {
                    > > originalArray.s plice(j, 1);
                    > > } else {
                    > > j++;
                    > > }
                    > > }
                    > > }
                    > > return originalArray;
                    > > }
                    > > </script>
                    > >
                    > > Actually, I'd make removeItems() a method of the Array object:
                    > >
                    > > Array.prototype .removeItems = function(itemsT oRemove) {
                    > > var j;
                    > > for (var i = 0; i < itemsToRemove.l ength; i++) {
                    > > j = 0;
                    > > while (j < this.length) {
                    > > if (this[j] == itemsToRemove[i]) {
                    > > this.splice(j, 1);
                    > > } else {
                    > > j++;
                    > > }
                    > > }
                    > > }
                    > > }
                    > > theArray.remove Items(toRemove) ;
                    > > alert(theArray) ;[/color][/color]

                    --
                    | Grant Wagner <gwagner@agrico reunited.com>

                    * Client-side Javascript and Netscape 4 DOM Reference available at:
                    * http://devedge.netscape.com/library/...ce/frames.html
                    * Internet Explorer DOM Reference available at:
                    * http://msdn.microsoft.com/workshop/a...ence_entry.asp
                    * Netscape 6/7 DOM Reference available at:
                    * http://www.mozilla.org/docs/dom/domref/
                    * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
                    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


                    Comment

                    • Dr John Stockton

                      #11
                      Re: Remove specified value from array

                      JRS: In article <2030665d.03072 20129.301f51aa@ posting.google. com>, seen
                      in news:comp.lang. javascript, Sam Collett <sam_collett@ly cos.co.uk>
                      posted at Tue, 22 Jul 2003 02:29:29 :-[color=blue]
                      >
                      >The next step is to try to remove an array of items from the original
                      >array.[/color]


                      There are two approaches.

                      The first is to loop through toRemove, with an inner loop of theArray.
                      The second is to loop through theArray, with an inner loop of toRemove.

                      IMHO, the first, potentially, has greater overheads than the second.

                      var theArray = [ 1,2,2,5,7,12,15 ,21 ]
                      var toRemove = [ 1,7 ]
                      var theNewArray = []

                      n = 0
                      for (j = 0 ; j < theArray.length ; j++) {
                      X = theArray[j] ; Wanted = true
                      for (k = 0 ; k < toRemove.length ; k++) {
                      if (X == toRemove[k]) { Wanted = false ; break } }
                      if (Wanted) theNewArray[n++] = X
                      }

                      To remove from the original array as actually asked, remove "New", and
                      finally do
                      theArray.length = n

                      Note that .splice() is not needed.

                      Certain optimisations can be made if the arrays are known to be sorted;
                      increase the initial value of k so as not to test for values which can
                      no longer occur, and cache the result of the k loop for re-use if X is
                      unchanged next time.

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                      Comment

                      • Dr John Stockton

                        #12
                        Re: Remove specified value from array

                        JRS: In article <pAR2MGHxooH$Ew 5a@merlyn.demon .co.uk>, seen in
                        news:comp.lang. javascript, Dr John Stockton <spam@merlyn.de mon.co.uk>
                        posted at Wed, 23 Jul 2003 14:14:25 :-
                        [color=blue]
                        >There are two approaches.
                        >
                        >The first is to loop through toRemove, with an inner loop of theArray.
                        >The second is to loop through theArray, with an inner loop of toRemove.
                        >
                        >IMHO, the first, potentially, has greater overheads than the second.
                        >
                        > var theArray = [ 1,2,2,5,7,12,15 ,21 ]
                        > var toRemove = [ 1,7 ]
                        > var theNewArray = []
                        >
                        > n = 0
                        > for (j = 0 ; j < theArray.length ; j++) {
                        > X = theArray[j] ; Wanted = true
                        > for (k = 0 ; k < toRemove.length ; k++) {
                        > if (X == toRemove[k]) { Wanted = false ; break } }
                        > if (Wanted) theNewArray[n++] = X
                        > }
                        >
                        >To remove from the original array as actually asked, remove "New", and
                        >finally do
                        > theArray.length = n[/color]

                        Moreover, in that the second approach, the visible inner loop (executed
                        for each element of the original array) can be removed by using an
                        initial loop executed once. AIUI, a bad Javascript implementation will
                        do the inner loop internally (which should be faster); a good one will
                        use a more effective search. The speed of the revised method will only
                        be minimally affected by duplicates in toRemove.

                        var theArray = [ 1,2,2,5,7,12,15 ,21 ]
                        var toRemove = [ 1,7 ]
                        var theNewArray = []

                        var Delenda = []
                        var U // Undefined value

                        for (j = 0 ; j < toRemove.length ; j++ ) Delenda[toRemove[j]] = true
                        n = 0
                        for (j = 0 ; j < theArray.length ; j++) {
                        X = theArray[j]
                        if (Delenda[X] === U) theNewArray[n++] = X
                        }



                        or, simplifying, ...

                        var Delenda = [], j, n = 0, T, U // Undefined value

                        for (j = 0 ; j < toRemove.length ; j++ )
                        Delenda[toRemove[j]] = true // or 0 !

                        for (j = 0 ; j < theArray.length ; j++ )
                        if ( Delenda[ T = theArray[j] ] === U ) theNewArray[n++] = T



                        There is no need for theArray or toRemove to be ordered, and order is
                        preserved; the elements do not need to be numeric.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                        Comment

                        • Sam Collett

                          #13
                          Re: Remove specified value from array

                          I am using a recordset with the original array in a field.

                          The removal array is from a series of checkboxes on a form (which is
                          also from the same field ion the recordset, so the order is the same).

                          I got the values using String(Request. Form("items")). split(",")

                          This returns values which include spaces (e.g. [17, 2]) which does not
                          compare properly for anything but the first value (17)

                          The solution was to use .split(", "). This suggests that forms submit
                          a series of values (e.g. from checkboxes) deliminated by ", " so I
                          have to take into account the space as well.

                          Now everthing is working as intended.

                          Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1EAA5D. 5EC8EFCF@agrico reunited.com>.. .[color=blue]
                          > Don't know what to tell you, I ran the following code in IE6SP1, Mozilla 1.5b, Netscape 4.78 and Opera 7.11 and
                          > they all generated [5, 675]:
                          >
                          > var theArray = [17,2,5,675];
                          > var toRemove = [17,2];
                          > // var toRemove = 5;
                          > Array.prototype .removeItems = function(itemsT oRemove) {
                          >
                          > if (!/Array/.test(itemsToRe move.constructo r)) {
                          > itemsToRemove = [ itemsToRemove ];
                          > }
                          >
                          > var j;
                          > for (var i = 0; i < itemsToRemove.l ength; i++) {
                          > j = 0;
                          > while (j < this.length) {
                          > if (this[j] == itemsToRemove[i]) {
                          > this.splice(j, 1);
                          > } else {
                          > j++;
                          > }
                          > }
                          > }
                          > }
                          > theArray.remove Items(toRemove) ;
                          > alert(theArray) ;
                          >
                          > There may be differences in the way Array.splice() acts in ASP and the way it's implemented in client-side
                          > JavaScript on the tested browsers. The arrays shouldn't need to be in any particular order for the code to work.
                          >
                          > Note I've also included some code requested by Greg that tests the removal parameter, so the method can now remove
                          > a single value or an array. It would be trival to also add functionality to allow: Array.removeIte ms(17, 2); in
                          > addition to Array.removeIte ms([17, 2]); and Array.removeIte ms(5);
                          >
                          > Sam Collett wrote:
                          >[color=green]
                          > > It only seems to remove the first item in the array (using the
                          > > prototype function), but using the removeItems function it performs as
                          > > expected.
                          > >
                          > > e.g.
                          > >
                          > > original array:[17,2,5,675]
                          > >
                          > > remove array:[17,2]
                          > >
                          > > resulting array:[2,5,675]
                          > >
                          > > Only the first item (17) seems to be removed from the array.
                          > >
                          > > Do both arrays need to be sorted, and if so how would I go about doing
                          > > that?
                          > >
                          > > Grant Wagner <gwagner@agrico reunited.com> wrote in message news:<3F1D8066. 2978531E@agrico reunited.com>.. .[color=darkred]
                          > > > Actually, given your initial variables:
                          > > >
                          > > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                          > > > var toRemove = [ 1,7 ]
                          > > > var theNewArray = removeItems(the Array, toRemove);
                          > > >
                          > > > The resulting array should be 2,2,5,12,15,21 not 1,5,12,15,21
                          > > >
                          > > > Anyway, use nested loops. Just repeat the original code for each value passed in:
                          > > >
                          > > > <script type="text/javascript">
                          > > > var theArray = [ 1,2,2,5,7,12,15 ,21 ];
                          > > > var toRemove = [ 1,7 ];
                          > > > var theNewArray = removeItems(the Array, toRemove);
                          > > > alert(theNewArr ay);
                          > > >
                          > > > function removeItems(ori ginalArray, itemsToRemove) {
                          > > > var j;
                          > > > for (var i = 0; i < itemsToRemove.l ength; i++) {
                          > > > j = 0;
                          > > > while (j < originalArray.l ength) {
                          > > > if (originalArray[j] == itemsToRemove[i]) {
                          > > > originalArray.s plice(j, 1);
                          > > > } else {
                          > > > j++;
                          > > > }
                          > > > }
                          > > > }
                          > > > return originalArray;
                          > > > }
                          > > > </script>
                          > > >
                          > > > Actually, I'd make removeItems() a method of the Array object:
                          > > >
                          > > > Array.prototype .removeItems = function(itemsT oRemove) {
                          > > > var j;
                          > > > for (var i = 0; i < itemsToRemove.l ength; i++) {
                          > > > j = 0;
                          > > > while (j < this.length) {
                          > > > if (this[j] == itemsToRemove[i]) {
                          > > > this.splice(j, 1);
                          > > > } else {
                          > > > j++;
                          > > > }
                          > > > }
                          > > > }
                          > > > }
                          > > > theArray.remove Items(toRemove) ;
                          > > > alert(theArray) ;[/color][/color]
                          >
                          > --
                          > | Grant Wagner <gwagner@agrico reunited.com>
                          >
                          > * Client-side Javascript and Netscape 4 DOM Reference available at:
                          > * http://devedge.netscape.com/library/...ce/frames.html
                          > * Internet Explorer DOM Reference available at:
                          > * http://msdn.microsoft.com/workshop/a...ence_entry.asp
                          > * Netscape 6/7 DOM Reference available at:
                          > * http://www.mozilla.org/docs/dom/domref/
                          > * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
                          > * http://www.mozilla.org/docs/web-deve...upgrade_2.html[/color]

                          Comment

                          Working...