Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Removing an element of an array

Reply
Thread Tools

Removing an element of an array

 
 
effendi@epitome.com.sg
Guest
Posts: n/a
 
      03-02-2005
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.

 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      03-02-2005
http://www.velocityreviews.com/forums/(E-Mail 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[i] = this[s + i];}
if(nI < dC) {
for(i = s, j = n - dC; i < j; ++i) {
this[i + nI] = this[i + dC];
}
} else if(nI > dC) {
for(i = n - 1, j = s + dC; i >= j; --i) {
this[i + nI - dC] = this[i];
}
}
for(i = s, j = 2; j < nI; ++i, ++j) {this[i] = arguments[j];}
this.length = n - dC + nI;
return a;
};
}

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

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

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

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

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

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
 
Dietmar Meier
Guest
Posts: n/a
 
      03-02-2005
Michael Winter wrote:

> 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[i];
}
return r;
}

Did I forget anything?

ciao, dhgm
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      03-02-2005
Dietmar Meier wrote:

> Michael Winter wrote:
>
>> 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 ).

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];
> }


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

[snip]

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

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      03-05-2005
Michael Winter wrote:

[snip]

> nI = Math.min(arguments.length - 2, 0),


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

[snip]

Mike

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing an element from an array laredotornado Java 5 10-26-2008 01:55 AM
Array Of Objects, Removing Element From Memory Larsenmtl Larsenmtl Ruby 3 08-07-2008 06:16 PM
Once again pointer to first element vs pointer to array cast topointer to element Szabolcs Borsanyi C Programming 6 05-23-2008 11:06 AM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Difficulty removing element from array Robert TV Perl Misc 3 09-21-2003 02:37 PM



Advertisments