Associate Array splice does not work


I am trying to understand why in nodejs array splice does not work on an associate array.

var a = []; a['x1'] = 234234; a['x2'] = 565464; console.log("Init-------"); showIt(); a.splice(0, 1); console.log("After splice-------"); showIt(); delete a['x1']; console.log("After delete-------"); showIt(); function showIt(){ var keys = Object.keys(a); var len = keys.length; var i=0; while (i < len) { console.log( ' ' + i + ' ------------ ' + keys[i] ); i++; } }


Init------- 0 ------------ x1 1 ------------ x2 After splice------- 0 ------------ x1 1 ------------ x2 After delete------- 0 ------------ x2

Splicing the array does nothing...

Same results in a browser...


Splice works as expected when the array is defined as:

var a = ['x1','x2','x3']; console.log("Init-------"); console.log(a); a.splice('x1', 1); console.log("After splice-------"); console.log(a);

Looks like in the first example, the array is being treated as if is was defined as a object {} in the 2nd, it's being treated more like an array.

To the Moderators:

This is not really a question about spare arrays, it is more of a question of an array which is starting at 0 and growing sequentially to 10 million over a period of days. As it is growing the array is being deleted from so that around 1000 items are in the array at one time.

I am considering forcing the use of hash tables by using non-numeric keys or defining as a object {} so that the it acts like a sparse array.

In the end, I am not sure if it matters...


In JavaScript there is no such thing as an associative array -- there are arrays (like normal arrays in other languages) and objects (like assoc. arrays in other languages). In your example a is a normal array but you set non-numerical keys on it, so the normal array methods (like splice) do not see it. They only look in the range 0...a.length.

Making a an object won't help; it is not possible to splice an object. Try using only numerical keys ([1] instead of ['x1']).


  • Replacing rownames of data frame by a sub-string
  • spirit x3 cannot propagate attributes of type optional
  • Check if coordinate in selected area
  • How to make a matlab legend recognize multiple scatter plots?
  • How can I count unique terms in a plaintext file case-insensitively?
  • Programmatically Update Linked Named Range of excel object in MS Word (2007)
  • Losing my session variables
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Get a trait object reference from a vector
  • Xmonad multiple submap key combos
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • Julia: How to give multiple workers access to functions that are 'include(…)' into a modul
  • php script is parsing content from RTE (tt_news) but internal links are not appearing as speaking ur
  • How can I mock ui-router's resolve values when testing a state's configuration?
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • what makes a request a new request in asp.net C#
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • How can I display the parent menu item's description using Wordpress walkers?
  • MySQL Order by column = x, column asc?
  • System.InvalidCastException: Specified cast is not valid
  • Is playing sound in Javascript performance heavy?
  • Remove final comma from string in vb.net
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • copying resource to sdcard gives a damaged file in android
  • Read a local file using javascript
  • Insert into database using onclick function
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Knitr HTML Loop - Some HTML output, some R output
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • retrieve vertices with no linked edge in arangodb
  • KeystoneJS: Relationships in Admin UI not updating
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF