47

If I have a JS associative array which is from what I gather is really an object, and I wish to remove an element, using delete myArr[someId] will set the element to undefined, whilst splice won't work at all... so what is the alternative for an associative array if I wish to delete an element (rather than setting it to undefined)

2
  • Why delete it? What is it hurting being in there? undefined is as good as it gets. You can make up a property for an established object and check its type, it will be undefined.
    – Jonathan M
    Nov 17, 2011 at 19:42
  • 1
    Are you asking the difference between delete myArray[someID] and myArray[someID]=undefined? If so, I think the former is better as the latter will lead to problems when checking for undefined
    – puk
    Nov 17, 2011 at 19:47

2 Answers 2

172

The terminology in js can be confusing at first, so lets straighten that out first.

Yes, pretty much everything in js is an object. However, there are differences in the data types.

An array can be used like as associative array, but its different than an object literal.

var x = []; //array
var y = {}; //object literal

An array is like a list. The keys of an array can be a numerical index or a string.

var x = ['a','b']; // x[0] === 'a', x[1] === 'b';
var x = [];
    x['one'] = 'a';
    x['blah'] = 'b'; 

Object literals are like dictionaries. They can be used in a similar way.

var x = { 0: 'a', 1: 'b' };
var x = { one: 'a', two: 'b' };

However, this is where you need to understand the differences.

You can use an array like an object literal, but you can't use an object literal quite like an array.

Arrays have the automated "length" property, This increments and decrements automatically based on the total number of elements in the array. You don't get this with object literals. Arrays also get all of the other array-specific methods like shift, unshift, splice, pop, push, etc. Object literals don't have those methods.

Lets talk about delete and what happens on an array and on an object literal.

var x = ['a', 'b']; //["a", "b"]
delete x[0]; //[undefined, "b"]

var x = {0:'1', 1:'b'}// { 0:"1", 1:"b"}
delete x[0]; // { 1:"b" }

If you perform a delete on an element of an array, the length of the array doesn't change. The element index is preserved and the value is set to 'undefined';

Conversely, performing a delete on an object literal removes the key/value from the object.

Finally, if you want to remove an element from an array.

var x = ['a', 'b']; 
x.splice(0,1); //modifies x. ['b']

So, in summary use delete on object literals. Use splice on arrays.

Hope this helps.

3
  • Splicing an array when items are defined one by one does not work... see stackoverflow.com/questions/21896167/… I am trying to understand what's happening here... Feb 20, 2014 at 1:23
  • !!! lets reconsider for arrays: jsperf.com/delete-vs-splice looks like Chrome 36 does the delete faster now, "Just in Time Compilation" helps here.
    – sushicutta
    Oct 29, 2014 at 10:12
  • @sushicutta the problem is not speed, the problem is what the length of the array is after removal.
    – Stephen S
    May 2, 2017 at 14:34
5

There is no other option. myArr["someCrazyIndexYouHaventPreviouslyUsed"] will return undefined; an associative array will always give you undefined for indexes that don't exist.

So delete myArr[someId] will cause myArr to treat someId like every other index that doesn't exist—isn't that what you want?

2
  • According to Geuis's answer, the array stores "undefined" as an actual value, and then, with this information in your answer, it also will output undefined for any undefined index, so it's a little different.
    – JVE999
    Jun 26, 2014 at 18:03
  • @JVE999 whether or not delete array[index] stores (or displays) undefined depends on the implementation of the JS engine. At least as of Firefox 51.0.1, !function(){var x=['a','b']; console.log(x); delete x[0]; console.log(x);}() returns Array [ "a", "b" ] Array [ <1 empty slot>, "b" ]
    – cychoi
    Mar 2, 2017 at 5:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.