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.
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.
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.
> > 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
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.
> 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.
Well, that "works", but now you're breaking "for (var i in array){...}"
--
Trond Michelsen
[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
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
}
<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
Regards,
Val
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
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.
> scrip...@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
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
> 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