Removing an element of an array

E

effendi

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

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

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

array[index]=""

This of course assigns null to the element

But there are two problems

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

Can anyone suggest a more effective way of doing this?

Thank you.
 
M

Michael Winter

(e-mail address removed) wrote:

[snip]
array[index]=""

This of course assigns null to the element

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

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

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

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

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

result = array.splice(2, 1);

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


result = array.splice(0, 2);

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


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

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


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

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

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

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

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

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

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

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

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

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

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

[snip]

Hope that helps,
Mike
 
D

Dietmar Meier

Michael said:
A complete implementation of splice is included below which can
supplement an older browser: [...]

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

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

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

Did I forget anything?

ciao, dhgm
 
M

Michael Winter

Dietmar said:
Michael said:
A complete implementation of splice is included below which can
supplement an older browser: [...]

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

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

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

+? || 0

with

Math.floor(+? || 0)

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

I don't think testing the Array constructor would be necessary, really.
Array.prototype.splice = mySplice;

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

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

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

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

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

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

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

[snip]
for (i=2; i<arguments.length; i++) {
this[this.length] = arguments;
}


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

[snip]

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

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top