Update 2018-07-21: For a long time, I have felt embarrassed about this answer, so I think it's time that I touch it up a little bit. Just a little commentary, clarification, and formatting to help expedite the reading of the needlessly long and convoluted parts of this answer.
THE SHORT VERSION
The actual answer to the question
As others have said, you can use delete
.
obj // {"foo": "bar"}
delete obj["foo"]
obj // {}
obj["foo"] // undefined
Array equivalent
Don't delete
from an array. Use Array.prototype.splice
instead.
arr // [1,2,3,4,5]
arr.splice(3,1); // 4
arr // [1,2,3,5]
THE LONG VERSION
JavaScript is an OOP Language, so everything is an object, including arrays. Thus, I feel it necessary to point out a particular caveat.
In arrays, unlike plain old objects, using delete
leaves behind garbage in the form of null
, creating a "hole" in the array.
var array = [1, 2, 3, 4];
delete array[2];
/* Expected result --> [1, 2, 4]
* Actual result --> [1, 2, null, 4]
*/
As you can see, delete
doesn't always work as one might expect. The value is overwritten, but the memory is not reallocated. That is to say, array[4]
isn't relocated to array[3]
. Which is in contrast to Array.prototype.unshift
, which inserts an element at the beginning of the array and shifts everything up (array[0]
becomes array[1]
, etc.)
Honestly, aside from setting to null
rather than undefined
--which is legitimately weird--this behavior shouldn't be surprising, since delete
is a unary operator, like typeof
, that is hard-boiled into the language and is not supposed to care about the type of object it's being used on, whereas Array
is a subclass of Object
with methods specifically designed for working with arrays. So there's no good reason for delete
to have a special case cooked in for re-shifting the array, as that would just slow things down with unnecessary work. In retrospect, my expectations were unrealistic.
Of course, it did surprise me. Because I wrote this to justify my crusade against "null garbage":
Ignoring the dangers and problems inherent in null
, and the space wasted, this can be problematic if the array needs to be precise.
Which is a terrible justification for getting rid of the null
s--null
is only dangerous if used improperly, and it has nothing to do with "precision". The real reason you shouldn't delete
from an array is that leaving garbage-filled and messy data structures around is sloppy and bug-prone.
What follows is a contrived scenario that gets pretty long-winded, so you can skip to the section, The Solution, if you want. The only reason I leave this section it is that I think some people probably think it's funny, and I don't want to be "that guy" who posts a "funny" answer and then deletes all the "funny" from it later on.
...It's stupid, I know.
The contrived and long-winded PDP-11 scenario
For example, say you are creating a webapp that uses JSON-serialization to store an array used for 'tabs' in a string (in this case, localStorage
). Let's also say that the code uses the numerical indices of the array's members to "title" them when drawing to the screen. Why are you doing this rather than just storing the "title" as well? Because... reasons.
Okay, let's just say that you're trying to save memory at the request of this one user who runs a PDP-11 minicomputer from the 1960's running UNIX and wrote his own Elinks-based, JavaScript-compliant, line-printer-friendly browser because X11 is out of the question.
Increasingly stupid edge-case scenario aside, using delete
on said array will result in null
polluting the array, and probably causing bugs in the app later on. And if you check for null
, it would straight up skip the numbers resulting in the tabs being rendered like [1] [2] [4] [5] ...
.
if (array[index] == null)
continue;
else
title = (index + 1).toString();
/* 0 -> "1"
* 1 -> "2"
* 2 -> (nothing)
* 3 -> "4"
*/
Yeah, that's definitely not what you wanted.
Now, you could keep a second iterator, like j
, to increment only when valid values are read from the array. But that wouldn't exactly solve the null
issue, and you still have to please that troll PDP-11 user. Alas, his computer just doesn't have enough memory to hold that last integer (don't ask how he manages to handle a variable-width array...).
So, he sends you an email in anger:
Hey, your webapp broke my browser! I checked my localStorage database after your stupid code made my browser segfault, and this is what I found:
>"tabs:['Hello World', 'foo bar baz', null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, ... ]"
After clearing my precious data, it segfaulted again, and I did a backtrace, and what do I find? WHAT DO I FIND!? YOU USE TOO MANY VARIABLES!
>var i = index;
>var j = 1;
Grr, I am angry now.
-Troll Davidson
About now, you're at your wit's end. This guy has been complaining non-stop about your app, and you want to tell him to shut up and go get a better computer.
The Solution: Array.prototype.splice
Luckily, arrays do have a specialized method for deleting indices and reallocating memory: Array.prototype.splice()
. You could write something like this:
Array.prototype.remove = function(index){
this.splice(index,1);
}
...
array = [1, 2, 3, 4];
array.remove(2);
// Result -> [1, 2, 4]
And just like that, you've pleased Mr. PDP-11. Hooray! (I'd still tell him off, though...)
Array.prototype.splice vs Array.prototype.slice
I feel it's important to point out the difference between these two similarly-named functions, as they are both very useful.
Array.prototype.splice(start, n)
.splice()
mutates the array, and returns the removed indices. The array is sliced starting from the index, start
, and n
elements are sliced out. If n is unspecified, the entire array after start
is sliced out (n = array.length - start
).
let a = [5,4,3,2,1];
let chunk = a.splice(2,2);
// a [5,4,3,2,1]
// start 0 1 2 - -
// n - - 1 2 -
chunk; // [3,2]
a; // [5,4,1]
Array.prototype.slice(start, end)
.slice()
is non-destructive and returns a new array containing the indicated indices from start
to end
. If end
is left unspecified, the behavior is the same as .splice()
(end = array.length
). The behavior is a bit tricky since, for some reason, end
indexes from 1 instead of 0. I don't know why it does this, but that's how it is. Also, if end <= start
, the result is an empty array.
let a = [5,4,3,2,1];
let chunks = [
a.slice(2,0),
a.slice(2,2),
a.slice(2,3),
a.slice(2,5) ];
// a [5,4,3,2,1]
// start 0 1 2 - -
// end, for... - - - - -
// chunks[0] 0 - - - - -
// chunks[1] 1 2 - - -
// chunks[2] 1 2 3 - -
// chunks[3] 1 2 3 4 5
chunks; // [ [], [], [3], [3,2,1] ]
a; // [5,4,3,2,1]
That actually isn't what's happening, but it's easier to think of that way. According to MDN, here's what's actually happening:
// a [5,4,3,2,1]
// start 0 1 2 - - -
// end, for... - - - - - -
// chunks[0] 0 - - - - -
// chunks[1] 0 1 2 - - -
// chunks[2] 0 1(2)3 - -
// chunks[3] 0 1(2 3 4)5
The index specified by end
is simply excluded from the slice. The parenthesized indices indicate what gets sliced. Either way, the behavior is not intuitive and it's bound to cause its fair share of off-by-one errors, so you might find it useful to make a wrapper function to more closely emulate the behavior of .splice()
:
function ez_slice(array, start = 0, n = null){
if(!Array.isArray(array) || !is_number(start))
return null;
if(is_number(n))
return array.slice(start, start + n);
if(n === null)
return array.slice(start);
return null;
}
ez_slice([5,4,3,2,1], 2, 1) // [3]
ez_slice([5,4,3,2,1], 2) // [3,2,1]
/* Fun fact: isNaN is unreliable.
* [NaN, [], {}, 0, 1, Infinity, undefined, null, "Hi"].filter(isNaN)
* [NaN, {}, undefined, "Hi"]
*
* What we want is...
*
* [NaN, [], {}, 0, 1, Infinity, undefined, null, "Hi"].filter(is_nan)
* [NaN]
*/
function is_nan(num){
return typeof num === "number"
&& num !== num;
}
function is_number(num){
return !is_nan(num)
&& typeof num === "number"
&& isFinite(num);
}
Note that the wrapper function is designed to be very strict about types, and will return null
if anything is off. That includes putting in a string like "3"
. It is left up to the programmer to be diligent about his types. This is to encourage good programming practice.
Update regarding is_array()
This is in regard to this (now-removed) snippet:
function is_array(array){
return array !== null
&& typeof array === "object"
&& typeof array.length !== "undefined"
&& array.__proto__ === Array.prototype;
}
So as it turns out, there actually IS a built-in way to tell if an array is truly an array, and that is Array.isArray()
, introduced in ECMAScript 5 (December 2009). I found this while looking to see if there was a question asking about telling arrays from objects, to see if there was either a better solution than mine, or to add mine if there were none. So, if you're using a version of JavaScript that is earlier than ECMA 5, there's your polyfill. However, I strongly recommend against using my is_array()
function, as continuing to support old versions of JavaScript means continuing to support the old browsers that implement them, which means encouraging the use of insecure software and putting users at risk for malware. So please, use Array.isArray()
. Use let
and const
. Use the new features that get added to the language. Don't use vendor prefixes. Delete that IE polyfill crap from your website. Delete that XHTML <!CDATA[[...
crap, too--we moved to HTML5 back in 2014. The sooner everybody withdraws support for those old/esoteric browsers, the sooner the browser vendors will actually follow the web standard and embrace the new technology, and the sooner we can move on to a more secure web.