By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

Is there any 'remove()' method in Array object?

P: n/a
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 Array('Lucas','Brasilino','Silva');
oName.remove('Brasilino');
oName.toString(); // puts out 'Lucas,Silva'

Any fancy function to me to add this as a 'remove()' method ??

Thanks a lot in advance.

Bests regards
Lucas Brasilino

Sep 5 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a

br*******@yahoo.com wrote:
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 Array('Lucas','Brasilino','Silva');
oName.remove('Brasilino');
oName.toString(); // puts out 'Lucas,Silva'

Any fancy function to me to add this as a 'remove()' method ??

Thanks a lot in advance.

Bests regards
Lucas Brasilino
You can use the Array.splice() method.

The splice method takes as arguments an index which you want to start
removing elements and the number of elements to remove. For example in
your case:

var arrNames = new Array("Lucas", "Brasilino", "Silva");
arrNames.splice(1, 1); //start at index 1, remove 1 element.

Sep 5 '06 #2

P: n/a

web.dev написав:
br*******@yahoo.com wrote:
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 Array('Lucas','Brasilino','Silva');
oName.remove('Brasilino');
oName.toString(); // puts out 'Lucas,Silva'

Any fancy function to me to add this as a 'remove()' method ??

Thanks a lot in advance.

Bests regards
Lucas Brasilino

You can use the Array.splice() method.

The splice method takes as arguments an index which you want to start
removing elements and the number of elements to remove. For example in
your case:

var arrNames = new Array("Lucas", "Brasilino", "Silva");
arrNames.splice(1, 1); //start at index 1, remove 1 element.
Probabely .splice() is the best choice.
Alternatively you can use "delete" keyword:
delete arrNames[1]
But then indexes in array wil be broken (0, and then 2, without 1)
and it would be possible to list the aray only using for(... in
....){...}

Sep 5 '06 #3

P: n/a
VK

sc********@gmail.com wrote:
Alternatively you can use "delete" keyword:
delete arrNames[1]
But then indexes in array wil be broken (0, and then 2, without 1)
Indexes will not be "broken" - they keep going from 0 to the existing
array length, but the array will get a non-instantiated (undefined)
member inside.

What OP is really looking for is not an Array but Vector data type but
JavaScript doesn't have such data type (though it can be emulated by
different means).
To stay withing the array mechanics slice/splice methods would be
indeed the best alternative.

Sep 6 '06 #4

P: n/a
sc********@gmail.com wrote:
web.dev напи ав:
>br*******@yahoo.com wrote:
<snip>
>>oName = new Array('Lucas','Brasilino','Silva');
oName.remove('Brasilino');
oName.toString(); // puts out 'Lucas,Silva'

Any fancy function to me to add this as a 'remove()' method ??
<snip>
Probabely .splice() is the best choice.
Except that the OP wants to identify the element(s) to be removed by
value not index.
Alternatively you can use "delete" keyword:
delete arrNames[1]
But then indexes in array wil be broken (0, and then 2,
without 1)
That is not broken for a javascript array, as they all have the
potential to be sparse.
and it would be possible to list the aray only using
for(... in ...){...}
Not true, the only consequences would be retrieving Undefined values for
the omitted property names.

Richard.
Sep 6 '06 #5

P: n/a

Richard Cornford написав:
sc********@gmail.com wrote:
web.dev Ð½Ð°Ð¿Ð¸Ñ Ð°Ð²:
br*******@yahoo.com wrote:
<snip>
>oName = new Array('Lucas','Brasilino','Silva');
oName.remove('Brasilino');
oName.toString(); // puts out 'Lucas,Silva'

Any fancy function to me to add this as a 'remove()' method ??
<snip>
Probabely .splice() is the best choice.

Except that the OP wants to identify the element(s) to be removed by
value not index.
I mean .splice() method is the best choice to solve that problem. Sure
he have to write function with linear search to remove by value. :-\
Alternatively you can use "delete" keyword:
delete arrNames[1]
But then indexes in array wil be broken (0, and then 2,
without 1)

That is not broken for a javascript array, as they all have the
potential to be sparse.
You may call it not broken, but after "delete" we can't just use
for(;;) without ifs.
and it would be possible to list the array only using
for(... in ...){...}

Not true, the only consequences would be retrieving Undefined values for
the omitted property names.
var arr=[1,2,3,4]
delete arr[1]
for(var i in arr){
alert(arr[i])
}
//see any undefined values?

Val

Sep 6 '06 #6

P: n/a
VK

sc********@gmail.com wrote:
That is not broken for a javascript array, as they all have the
potential to be sparse.
You may call it not broken, but after "delete" we can't just use
for(;;) without ifs.
Sure we can.

var a = [1,2,3];
delete a[0];
for (var i=0;i<a.length;i++) {
alert(a[i] || 'undefined');
}

var arr=[1,2,3,4]
delete arr[1]
for(var i in arr){
alert(arr[i])
}
//see any undefined values?
No, I don't. But I don't see any array here neither. You are
enumerating the properties of arr *Object*. That was a FAQ actually a
while ago. To get on hold of the problem, try this:

var a = [1,2,3];
delete a[0];
a.foo = 'bar';
for (var i=0;i<a.length;i++) {
alert(a[i] || 'undefined');
}
for (var p in a) {
alert (a[p]);
}

Enumerating properties of the underlaying Object rather than dealing
with the Array is irritating for professional eyes, but not a crime and
is still in use: for exactly the reason I spelled in my previous post -
because JavaScript doesn't have Vector data type. So for-in loop on
array is a quick'n'durty way to avoid unnecessary loops in sparse
arrays.
The main task here is to understand clearly when are you dealing with
Array and when with its Object prototype - to avoid asking questions
like "why do I get a wrong array length?", "where are my array
members?" and such.
I'm not telling at all that you do not have such understanding.

Sep 6 '06 #7

P: n/a
sc********@gmail.com wrote:
Richard Cornford написав:
sc********@gmail.com wrote:
web.dev Ð½Ð°Ð¿Ð¸Ñ Ð°Ð²:
>br*******@yahoo.com wrote:
<snip>
>>oName = new Array('Lucas','Brasilino','Silva');
>>oName.remove('Brasilino');
>>oName.toString(); // puts out 'Lucas,Silva'
>>>
>>Any fancy function to me to add this as a 'remove()' method ??
<snip>
Probabely .splice() is the best choice.
Except that the OP wants to identify the element(s) to be removed by
value not index.
I mean .splice() method is the best choice to solve that problem. Sure
he have to write function with linear search to remove by value. :-\
Alternatively you can use "delete" keyword:
delete arrNames[1]
But then indexes in array wil be broken (0, and then 2,
without 1)
That is not broken for a javascript array, as they all have the
potential to be sparse.
You may call it not broken, but after "delete" we can't just use
for(;;) without ifs.
As arrays can explicitly store an Undefined value and may be sparse
anyway any "ifs" don't differ from those that normally apply to the use
of an Array in javascript.
>>and it would be possible to list the array only using
for(... in ...){...}

Not true, the only consequences would be retrieving Undefined values for
the omitted property names.
var arr=[1,2,3,4]
delete arr[1]
for(var i in arr){
alert(arr[i])
}
//see any undefined values?
That is using the - for-in - loop that you erroneously asserted "would
be possible to list the array only using".

A normal - for - loop limited by the array length will show Undefined
values, but then so would:-

var arr = [undefined, undefined, undefined];
alert(arr.length)
delete arr[1];
for(var prop in arr){
alert('arr['+prop+'] = '+arr[prop]);
}

- so where is the restriction to only using - for-in - following a
deletion?

Richard.

Sep 6 '06 #8

P: n/a

br*******@yahoo.com написав:
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 Array('Lucas','Brasilino','Silva');
oName.remove('Brasilino');
oName.toString(); // puts out 'Lucas,Silva'

Any fancy function to me to add this as a 'remove()' method ??

Thanks a lot in advance.
Array.prototype.remove=function(value){
for(var i=0;i<this.length;i++){
if (this[i]===value){
this.splice(i,1)
return this
}
}
}

Sep 6 '06 #9

P: n/a
sc********@gmail.com wrote:
br*******@yahoo.com написав:
>Any fancy function to me to add this as a 'remove()' method ??
Array.prototype.remove=function(value){
for(var i=0;i<this.length;i++){
if (this[i]===value){
this.splice(i,1)
return this
}
}
}
Well, that "works", but now you're breaking "for (var i in array){...}"

--
Trond Michelsen
Sep 6 '06 #10

P: n/a
sc********@gmail.com wrote:

[snip]
Array.prototype.remove=function(value){
for(var i=0;i<this.length;i++){
if (this[i]===value){
this.splice(i,1)
return this
}
}
}
That seems a little ill-conceived. Wouldn't it be better to:

- break out of the loop and always return the this operator
value.
- return the value of the removed element (should it be found).
- return true if an element was removed, or false otherwise.
- return the length of the object referred to by the this
operator (similar to the first alternative).

Only returning the this operator value upon successful removal makes
chaining method calls awkward, and object/undefined makes a poor
substitute for boolean values.

You should also store the value of the length property, rather than look
it up on each iteration.

Mike
Sep 6 '06 #11

P: n/a

Michael Winter написав:
sc********@gmail.com wrote:

[snip]
Array.prototype.remove=function(value){
for(var i=0;i<this.length;i++){
if (this[i]===value){
this.splice(i,1)
return this
}
}
}

That seems a little ill-conceived. Wouldn't it be better to:

- break out of the loop and always return the this operator
value.
- return the value of the removed element (should it be found).
- return true if an element was removed, or false otherwise.
- return the length of the object referred to by the this
operator (similar to the first alternative).

Only returning the this operator value upon successful removal makes
chaining method calls awkward, and object/undefined makes a poor
substitute for boolean values.

You should also store the value of the length property, rather than look
it up on each iteration.

Mike
Mike, you 100% right.
So where is your version of code? ;)

Array.prototype.remove=function(value){
var len=this.length
for(var i=0;i<len;i++){
if (this[i]===value){
this.splice(i,1)
break
}
}
return this
}

Sep 6 '06 #12

P: n/a

Trond Michelsen написав:
sc********@gmail.com wrote:
br*******@yahoo.com написав:
Any fancy function to me to add this as a 'remove()' method ??
Array.prototype.remove=function(value){
for(var i=0;i<this.length;i++){
if (this[i]===value){
this.splice(i,1)
return this
}
}
}

Well, that "works", but now you're breaking "for (var i in array){...}"

--
Trond Michelsen
Example, please.

Sep 6 '06 #13

P: n/a
sc********@gmail.com wrote:
Trond Michelsen написав:
>sc********@gmail.com wrote:
>>>Any fancy function to me to add this as a 'remove()' method ??
Array.prototype.remove=function(value){
[...]
>>}
Well, that "works", but now you're breaking "for (var i in array){...}"
Example, please.
<script type="text/javascript">
Array.prototype.remove=function(value){
for(var i=0;i<this.length;i++){
if (this[i]===value){
this.splice(i,1)
return this
}
}
}
var array = ["foo", "bar", "baz"];
for (var i in array) {
document.write("<b>" + i + ":</b" + array[i] + "<br>\n");
}
</script>

http://crusaders.no/~trondmm/arraydelete.html

--
Trond Michelsen
Sep 6 '06 #14

P: n/a

Richard Cornford написав:
sc********@gmail.com wrote:
Richard Cornford написав:
sc********@gmail.com wrote:
web.dev Ð½Ð°Ð¿Ð¸Ñ Ð°Ð²:
br*******@yahoo.com wrote:
<snip>
>oName = new Array('Lucas','Brasilino','Silva');
>oName.remove('Brasilino');
>oName.toString(); // puts out 'Lucas,Silva'
>>
>Any fancy function to me to add this as a 'remove()' method ??
<snip>
Probabely .splice() is the best choice.
>
Except that the OP wants to identify the element(s) to be removed by
value not index.
I mean .splice() method is the best choice to solve that problem. Sure
he have to write function with linear search to remove by value. :-\
Alternatively you can use "delete" keyword:
delete arrNames[1]
But then indexes in array wil be broken (0, and then 2,
without 1)
>
That is not broken for a javascript array, as they all have the
potential to be sparse.
You may call it not broken, but after "delete" we can't just use
for(;;) without ifs.

As arrays can explicitly store an Undefined value and may be sparse
anyway any "ifs" don't differ from those that normally apply to the use
of an Array in javascript.
>and it would be possible to list the array only using
for(... in ...){...}

Not true, the only consequences would be retrieving Undefined values for
the omitted property names.
var arr=[1,2,3,4]
delete arr[1]
for(var i in arr){
alert(arr[i])
}
//see any undefined values?

That is using the - for-in - loop that you erroneously asserted "would
be possible to list the array only using".

A normal - for - loop limited by the array length will show Undefined
values, but then so would:-

var arr = [undefined, undefined, undefined];
alert(arr.length)
delete arr[1];
for(var prop in arr){
alert('arr['+prop+'] = '+arr[prop]);
}

- so where is the restriction to only using - for-in - following a
deletion?

Richard.
Yes, you right.
I think we are too far now from first post in this topic. Maybe you
would propose your solution?
I've done it already.

Regards,
Val

Sep 6 '06 #15

P: n/a

Trond Michelsen написав:
sc********@gmail.com wrote:
Trond Michelsen написав:
sc********@gmail.com wrote:
Any fancy function to me to add this as a 'remove()' method ??
Array.prototype.remove=function(value){
[...]
>}
Well, that "works", but now you're breaking "for (var i in array){...}"
Example, please.

<script type="text/javascript">
Array.prototype.remove=function(value){
for(var i=0;i<this.length;i++){
if (this[i]===value){
this.splice(i,1)
return this
}
}
}
var array = ["foo", "bar", "baz"];
for (var i in array) {
document.write("<b>" + i + ":</b" + array[i] + "<br>\n");
}
</script>
You right, but it is the problem with all extensions methods for built
in objects. If you so like to use for/in with arrays:

function removeFromArray(value,arr){
var len=arr.length
for(var i=0;i<len;i++){
if (arr[i]===value){
arr.splice(i,1)
break
}
}
return arr
}

Now it is ok?

Regards,
Val

Sep 6 '06 #16

P: n/a
sc********@gmail.com wrote:
Richard Cornford написав:
<snip>
>so where is the restriction to only using - for-in - following a
deletion?
Yes, you right.
I think we are too far now from first post in this topic. Maybe you
would propose your solution?
The OP seems to want to remove items from an Array by value, but has
not stated whether this is to be the first item with the offending
value, or all items. Either way the code to do that is trivial but to
klunky and ugly to seem like the way to approach any problem. The
actual problem that needs solving has not been stated, only the OP's
notion of part of how it might be solved. Without a clear specification
form the OP everyone is reduced to guessing, wasting their time if they
guess wrong, and possibly wasting the OP's if they guess right but the
result is a poor approach to the real problem.
I've done it already.
The OP seems to want to remove items from an Array by value, but has
not stated whether this is to be the first item with the offending
value, or all items. Either way the code to do that is trivial but to
klunky and ugly to seem like the way to approach any problem. The
actual problem that needs solving has not been stated, only the OP's
notion of part of how it might be solved. Without a clear specification
form the OP everyone is reduced to guessing, wasting their time if they
guess wrong, and possibly wasting the OP's if they guess right but the
result is a poor approach to the real problem.

Richard.

Sep 6 '06 #17

P: n/a
Trond Michelsen wrote:
sc********@gmail.com wrote:
[snip]
>Array.prototype.remove=function(value){
[snip]
Well, that "works", but now you're breaking "for (var i in array){...}"
Breaking it? I think not. The for..in statement allows one to iterate
over all enumerable properties, which includes all user-defined
properties. What's broken about clearly defined behaviour?

The "What about for..in?" argument has never been very persuasive (to
me, at least). Most of the time, one doesn't use the statement at all,
let alone with arrays. It would only be (slightly more) significant if
the added prototype property is of the same type (function, in this
case) as the other, desirable enumerable properties, and even then,
there are still ways around it (including not using for..in at all).

Mike
Sep 6 '06 #18

P: n/a
Michael Winter wrote:
sc********@gmail.com wrote:
That seems a little ill-conceived. Wouldn't it be better to:
- break out of the loop and always return the this operator
value.
What if the value exists in multiple places in the array?
- return the value of the removed element (should it be found).
- return true if an element was removed, or false otherwise.
- return the length of the object referred to by the this
operator (similar to the first alternative).
What if multiple are found? Maybe return a count instead?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 6 '06 #19

P: n/a
Matt Kruse wrote:
Michael Winter wrote:
>That seems a little ill-conceived. Wouldn't it be better to:
- break out of the loop and always return the this operator
value.

What if the value exists in multiple places in the array?
Then you wouldn't break out. :-P

As Richard wrote, the OP didn't specify the actions of the method very
well, so there are a variety of approaches that could be taken...

[snip]
What if multiple are found? Maybe return a count instead?
....and returning an integer specifying the number of removed elements,
rather than a simple boolean, is certainly another candidate.

Mike
Sep 6 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.