473,387 Members | 1,290 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,387 software developers and data experts.

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

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
19 6279

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

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
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
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

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
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
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

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
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
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

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

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
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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Sam Collett | last post by:
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...
7
by: Voetleuce en fnsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
6
by: Rimuen | last post by:
Have a string contains numbers from database. But there is similar numbers want to remove Example: 1,3,6,6,6,12,13,14,15,15,15,15 Want to remove the similar numbers so it would be like:...
1
by: Ryan McLean | last post by:
Hi everyone! What is happening is the method: sub_btnSubmitClicked is being executed every time any other object with a Handler is executed. I am trying not to use the withevents and handles...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
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...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
6
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.