3

I have array with integer index

myarray[1]=val1
myarray[2]=val2
myarray[4]=val3
myarray[5]=val4

in this case myarray[0] and myarray[3] is undefined and

for (var i in myarray)
{
 console.log(i)
}

output 1,2,4,5

I like to "remove" myarray[2] without shifting others members and that after delete also myarray[2] come to be undefined and output should be 1,4,5

I tred myarray.splice(2,1,undefined) but it's still output index 2

5
  • Is there a reason you can't just do this: myArray[2] = undefined;? Commented Oct 2, 2013 at 21:53
  • What do you mean by "without shifting other members"? Commented Oct 2, 2013 at 21:53
  • @rescuecreative There is a difference, you can remove a key completely, or set its value to undefined. See stackoverflow.com/questions/18947892/… Commented Oct 2, 2013 at 21:56
  • you could use 'delete' to remove the property 2 or use splice(2,1) without the third argument. Commented Oct 2, 2013 at 21:59
  • @bfavaretto You're right. I realized that after I tested Barmar's answer. Commented Oct 2, 2013 at 21:59

3 Answers 3

6

Stop stop!

First problem, while yes most things in js are objects, do not iterate over arrays with for in. "for ... in" is for object literals. Use a normal iterative loop for arrays.

Second, delete is intended to delete properties on objects. Again, arrays are objects but this isn't the right use of delete.

If you want to set the value of an array index to undefined, just assign it undefined.

myarray[2] = undefined;

Edit:

The reason you don't use for...in on an array is this:

var x = [1,2,3];
x.test = 'abc';

for(var i in x){
    console.log(x[i]);
}
//1,2,3,abc

for(var i=0; i<x.length; i++){
   console.log(x[i]);
}
//1,2,3

What Dmitry seems to be trying to do is only return indices that have values. Do it like this.

for(var i=0; i<myarray.length; i++){
    if( myarray[i] !== undefined ){
        console.log( myarray[i] );
    }
}

Again, to set an index to undefined you shouldn't use delete. You can, but don't. Just do:

myarray[i] = undefined;

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

But look at the desired output. Sounds like a hacky solution is exactly what the OP is looking for.
Honestly I can't really understand what's being asked but I'll amend my answer with what I think he's asking about. You should almost never iterate over an array with for...in. It will return any property on the object in addition to the numerical keys.
Any non-enumerable property, yes. What I'm trying to say is that a sparse array seems to be desired in this case. The warnings against for..in are never undesirable though.
I also try to avoid checking is value is undefined in case of for .. in get only relevant indexes and don't use if( myarray[i] !== undefined ) is
4

Use delete:

delete myarray[2];

1 Comment

actually delete provide me required result.
0
delete myArray[2];

Use delete to remove an array index.

>> myArray[2]
undefined

And now you won't iterate over it with your for-in loop.

Also, for-in should only be used for iterating over object's properties. Use a regular for-loop when iterating over arrays.

2 Comments

what is a problem to use forr ... in for iteration over array indexes ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.