473,288 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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 55505
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
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
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
3
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
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
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
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
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
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
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
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.