How do I unset an element in an array in javascript?

JavascriptArrays

Javascript Problem Overview


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);}

Javascript Solutions


Solution 1 - Javascript

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);
    }
}

Solution 2 - Javascript

delete foo[key];

:D

Solution 3 - Javascript

If you know the key name simply do like this:

delete array['key_name']

Solution 4 - Javascript

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.

Solution 5 - Javascript

This is how I would do it

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

Solution 6 - Javascript

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

Solution 7 - Javascript

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

Solution 8 - Javascript

Array element unset

  • Using pop
var ar = [1, 2, 3, 4, 5, 6];
ar.pop(); // returns 6
console.log( ar ); // [1, 2, 3, 4, 5]
  • Using shift
var ar = ['zero', 'one', 'two', 'three'];
ar.shift(); // returns "zero"
console.log( ar ); // ["one", "two", "three"]
  • Using splice
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var removed = arr.splice(2,2);

var list = ["bar", "baz", "foo", "qux"];
    
list.splice(0, 2); 
// Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"].

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteven NobleView Question on Stackoverflow
Solution 1 - JavascriptgoingView Answer on Stackoverflow
Solution 2 - Javascriptuser19302View Answer on Stackoverflow
Solution 3 - Javascriptuser3177525View Answer on Stackoverflow
Solution 4 - JavascriptJohn FactorialView Answer on Stackoverflow
Solution 5 - JavascriptstackoverflowsView Answer on Stackoverflow
Solution 6 - JavascriptlingView Answer on Stackoverflow
Solution 7 - JavascriptAlexeiOstView Answer on Stackoverflow
Solution 8 - JavascriptMD SHAYONView Answer on Stackoverflow