473,503 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(reRemove,"")
// remove if at start of array
reRemove = new RegExp("^"+val+",")
s = s.replace(reRemove,"")
// remove if only item
reRemove = new RegExp("^"+val+"$")
s = s.replace(reRemove,"")
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"
Jul 20 '05 #1
12 55526
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(reRemove,"")
// remove if at start of array
reRemove = new RegExp("^"+val+",")
s = s.replace(reRemove,"")
// remove if only item
reRemove = new RegExp("^"+val+"$")
s = s.replace(reRemove,"")
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(theArray, 2);
alert(theNewArray.join(", "));

function removeItems(array, 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,15,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 <gw*****@agricoreunited.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
Jul 20 '05 #2
> 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(reRemove,"")
// remove if at start of array
reRemove = new RegExp("^"+val+",")
s = s.replace(reRemove,"")
// remove if only item
reRemove = new RegExp("^"+val+"$")
s = s.replace(reRemove,"")
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"


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.

http://www.crockford.com/javascript/remedial.html

Jul 20 '05 #3
JRS: In article <20**************************@posting.google.com >, seen
in news:comp.lang.javascript, Sam Collett <sa*********@lycos.co.uk>
posted at Mon, 21 Jul 2003 07:14:54 :-
How do I remove an item with a specified value from an array?


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.demon.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)
Jul 20 '05 #4
JRS: In article <bf**********@sun-news.laserlink.net>, seen in
news:comp.lang.javascript, Douglas Crockford <no****@laserlink.net>
posted at Mon, 21 Jul 2003 10:56:26 :-
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);


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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5
Actually, given your initial variables:

var theArray = [ 1,2,2,5,7,12,15,21 ];
var toRemove = [ 1,7 ]
var theNewArray = removeItems(theArray, 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(theArray, toRemove);
alert(theNewArray);

function removeItems(originalArray, itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < originalArray.length) {
if (originalArray[j] == itemsToRemove[i]) {
originalArray.splice(j, 1);
} else {
j++;
}
}
}
return originalArray;
}
</script>

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

Array.prototype.removeItems = function(itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < this.length) {
if (this[j] == itemsToRemove[i]) {
this.splice(j, 1);
} else {
j++;
}
}
}
}
theArray.removeItems(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:
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(theArray, 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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
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(reRemove,"")
// remove if at start of array
reRemove = new RegExp("^"+val+",")
s = s.replace(reRemove,"")
// remove if only item
reRemove = new RegExp("^"+val+"$")
s = s.replace(reRemove,"")
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(theArray, 2);
alert(theNewArray.join(", "));

function removeItems(array, 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,15,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 <gw*****@agricoreunited.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
Jul 20 '05 #6
sa*********@lycos.co.uk (Sam Collett) wrote in message news:<20**************************@posting.google. com>...
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(theArray, 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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
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(reRemove,"")
// remove if at start of array
reRemove = new RegExp("^"+val+",")
s = s.replace(reRemove,"")
// remove if only item
reRemove = new RegExp("^"+val+"$")
s = s.replace(reRemove,"")
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(theArray, 2);
alert(theNewArray.join(", "));

function removeItems(array, 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,15,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 <gw*****@agricoreunited.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


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>
Jul 20 '05 #7
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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
Actually, given your initial variables:

var theArray = [ 1,2,2,5,7,12,15,21 ];
var toRemove = [ 1,7 ]
var theNewArray = removeItems(theArray, 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(theArray, toRemove);
alert(theNewArray);

function removeItems(originalArray, itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < originalArray.length) {
if (originalArray[j] == itemsToRemove[i]) {
originalArray.splice(j, 1);
} else {
j++;
}
}
}
return originalArray;
}
</script>

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

Array.prototype.removeItems = function(itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < this.length) {
if (this[j] == itemsToRemove[i]) {
this.splice(j, 1);
} else {
j++;
}
}
}
}
theArray.removeItems(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:
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(theArray, 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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
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(reRemove,"")
> // remove if at start of array
> reRemove = new RegExp("^"+val+",")
> s = s.replace(reRemove,"")
> // remove if only item
> reRemove = new RegExp("^"+val+"$")
> s = s.replace(reRemove,"")
> 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(theArray, 2);
alert(theNewArray.join(", "));

function removeItems(array, 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,15,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 <gw*****@agricoreunited.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

Jul 20 '05 #8
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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
Actually, given your initial variables:

var theArray = [ 1,2,2,5,7,12,15,21 ];
var toRemove = [ 1,7 ]
var theNewArray = removeItems(theArray, 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(theArray, toRemove);
alert(theNewArray);

function removeItems(originalArray, itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < originalArray.length) {
if (originalArray[j] == itemsToRemove[i]) {
originalArray.splice(j, 1);
} else {
j++;
}
}
}
return originalArray;
}
</script>

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

Array.prototype.removeItems = function(itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < this.length) {
if (this[j] == itemsToRemove[i]) {
this.splice(j, 1);
} else {
j++;
}
}
}
}
theArray.removeItems(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:
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(theArray, 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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
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(reRemove,"")
> // remove if at start of array
> reRemove = new RegExp("^"+val+",")
> s = s.replace(reRemove,"")
> // remove if only item
> reRemove = new RegExp("^"+val+"$")
> s = s.replace(reRemove,"")
> 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(theArray, 2);
alert(theNewArray.join(", "));

function removeItems(array, 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,15,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 <gw*****@agricoreunited.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

Jul 20 '05 #9
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(itemsToRemove) {

if (!/Array/.test(itemsToRemove.constructor)) {
itemsToRemove = [ itemsToRemove ];
}

var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < this.length) {
if (this[j] == itemsToRemove[i]) {
this.splice(j, 1);
} else {
j++;
}
}
}
}
theArray.removeItems(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.removeItems(17, 2); in
addition to Array.removeItems([17, 2]); and Array.removeItems(5);

Sam Collett wrote:
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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
Actually, given your initial variables:

var theArray = [ 1,2,2,5,7,12,15,21 ];
var toRemove = [ 1,7 ]
var theNewArray = removeItems(theArray, 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(theArray, toRemove);
alert(theNewArray);

function removeItems(originalArray, itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < originalArray.length) {
if (originalArray[j] == itemsToRemove[i]) {
originalArray.splice(j, 1);
} else {
j++;
}
}
}
return originalArray;
}
</script>

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

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


--
| Grant Wagner <gw*****@agricoreunited.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
Jul 20 '05 #10
JRS: In article <20**************************@posting.google.com >, seen
in news:comp.lang.javascript, Sam Collett <sa*********@lycos.co.uk>
posted at Tue, 22 Jul 2003 02:29:29 :-

The next step is to try to remove an array of items from the original
array.

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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #11
JRS: In article <pA**************@merlyn.demon.co.uk>, seen in
news:comp.lang.javascript, Dr John Stockton <sp**@merlyn.demon.co.uk>
posted at Wed, 23 Jul 2003 14:14:25 :-
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


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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #12
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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
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(itemsToRemove) {

if (!/Array/.test(itemsToRemove.constructor)) {
itemsToRemove = [ itemsToRemove ];
}

var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < this.length) {
if (this[j] == itemsToRemove[i]) {
this.splice(j, 1);
} else {
j++;
}
}
}
}
theArray.removeItems(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.removeItems(17, 2); in
addition to Array.removeItems([17, 2]); and Array.removeItems(5);

Sam Collett wrote:
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 <gw*****@agricoreunited.com> wrote in message news:<3F***************@agricoreunited.com>...
Actually, given your initial variables:

var theArray = [ 1,2,2,5,7,12,15,21 ];
var toRemove = [ 1,7 ]
var theNewArray = removeItems(theArray, 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(theArray, toRemove);
alert(theNewArray);

function removeItems(originalArray, itemsToRemove) {
var j;
for (var i = 0; i < itemsToRemove.length; i++) {
j = 0;
while (j < originalArray.length) {
if (originalArray[j] == itemsToRemove[i]) {
originalArray.splice(j, 1);
} else {
j++;
}
}
}
return originalArray;
}
</script>

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

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


--
| Grant Wagner <gw*****@agricoreunited.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

Jul 20 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4536
by: mike | last post by:
I am using IE and I want to remove some attributes on my page before I delete the rows as a result of what looks like memory leaks in my page. I understand I can't remove the attributes, but the...
4
9785
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
3
6260
by: craigbennett | last post by:
Hi, I am trying to use std::remove to delete a file and have been encountering some problems. My goal is to pass std::remove a std::string and remove the file of that name, for example. ...
4
71101
by: None | last post by:
Hi, I have declared array as Int ids = new int; In ArrayList we can remove specified index using RemoveAt(5) method. My question is how can we do this one with int array (single dimensional...
3
5380
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
19
6290
by: brasilino | last post by:
Hi Folks: I've been looking (aka googling) around with no success. I need a usability beyond 'pop()' method when removing an Array elements. For example: oName = new...
0
1169
by: ClassicNancy | last post by:
If I want to remove the drop down for the year will that make the birthday not show on the calendar? If it looks like it is ok to remove it what can be removed?? What should it look like? ...
6
7962
by: Romulo NF | last post by:
Greetings again to everyone, Im back to show this grid componenet i´ve developed. With this grid you can show the data like a normal table, remove the rows that you need, add rows, import data,...
36
9135
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have an array of strings. What is the simplest way to remove the elements that are empty, i.e. where the expression "empty($elt)" returns true? Thanks, - Dave
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.