170

How do I remove the key 'bar' from an array foo so that 'bar' won't show up in

for(key in foo){alert(key);}
0

7 Answers 7

289

Don't use delete as it won't remove an element from an array it will only set it as undefined, which will then not be reflected correctly in the length of the array.

If you know the key you should use splice i.e.

myArray.splice(key, 1);

For someone in Steven's position you can try something like this:

for (var key in myArray) {
    if (key == 'bar') {
        myArray.splice(key, 1);
    }
}

or

for (var key in myArray) {
    if (myArray[key] == 'bar') {
        myArray.splice(key, 1);
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

+1 Good point, I also like this way: ejohn.org/blog/javascript-array-remove
@ThiefMaster - Care to explain? I don't know what that means and keen to learn. As always, an edit is a 1,000,000 times better than an off-hand comment; you should know that with the amount of rep you have.
Use for (var key in myArray) instead of for (key in myArray) - otherwise key is a global variable and if you call a function inside that loop that has the same problem, you will get unexpected results.
Note, the second example doesn't work if the array has multiple items in a row that match 'bar'. The index of the array will shift and you will miss splicing half of the items. Instead, use a for loop that decrements the index when a match is found.
|
69
delete foo[key];

:D

5 Comments

technically that doesn't answer the question. he's looking to remove the key 'bar'. and your key variable doesn't imply that. :P
key='bar'; eval("delete foo."+key); hides ;D
Do NOT use eval. Deleting a key/index of an object/array can be achieved by much simpler methods not using eval.
Actually if you remove from indexed array by delete foo[key] it will stores value in foo[key]=undefined and foo.length will remain same which is wrong. So use SPLICE instead
use delete foo[key] it wrong; should use foo.splice(key, 1);
43

If you know the key name simply do like this:

delete array['key_name']

1 Comment

This is not an array! It's an object (yes, these are called associative arrays in JS, but no, they aren't really, and this method is harmful if you trully work with a JS array).
38

An important note: JavaScript Arrays are not associative arrays like those you might be used to from PHP. If your "array key" is a string, you're no longer operating on the contents of an array. Your array is an object, and you're using bracket notation to access the member named <key name>. Thus:

var myArray = [];
myArray["bar"] = true;
myArray["foo"] = true;
alert(myArray.length); // returns 0.

because you have not added elements to the array, you have only modified myArray's bar and foo members.

Comments

6

This is how I would do it

 myArray.splice( myArray.indexOf('bar') , 1) 

1 Comment

The Array.prototype.indexOf() method searches for a value within an array, not for a key. So this will not work.
1

http://www.internetdoc.info/javascript-function/remove-key-from-array.htm

removeKey(arrayName,key);

function removeKey(arrayName,key)
{
 var x;
 var tmpArray = new Array();
 for(x in arrayName)
 {
  if(x!=key) { tmpArray[x] = arrayName[x]; }
 }
 return tmpArray;
}

Comments

1

there is an important difference between delete and splice:

ORIGINAL ARRAY:

[<1 empty item>, 'one',<3 empty items>, 'five', <3 empty items>,'nine']

AFTER SPLICE (array.splice(1,1)):

[ <4 empty items>, 'five', <3 empty items>, 'nine' ]

AFTER DELETE (delete array[1]):

[ <5 empty items>, 'five', <3 empty items>, 'nine' ]

Comments

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.