admin 管理员组

文章数量: 1086019

I currently have a problem in deleting entries from an associative array in JS.

I tried this:

  myArray['key'] = value;
  myArray['key1'] = value1;

  ...

  delete myArray['key'];

But I get following results in my application:

  [ undefined, { key1: 'value1', key2: 'value2' }, undefined,
    { key1: 'value1', key2: 'value2' }, undefined, undefined ]

How can I delete the whole entry, key and value? I found the method splice() but I think it uses a different index. I wasn't able to delete the entries I want by passing the key to splice().

I currently have a problem in deleting entries from an associative array in JS.

I tried this:

  myArray['key'] = value;
  myArray['key1'] = value1;

  ...

  delete myArray['key'];

But I get following results in my application:

  [ undefined, { key1: 'value1', key2: 'value2' }, undefined,
    { key1: 'value1', key2: 'value2' }, undefined, undefined ]

How can I delete the whole entry, key and value? I found the method splice() but I think it uses a different index. I wasn't able to delete the entries I want by passing the key to splice().

Share Improve this question edited Mar 16, 2014 at 10:59 ffraenz asked May 23, 2011 at 19:15 ffraenzffraenz 6602 gold badges10 silver badges35 bronze badges 1
  • Javascript doesn't have associative arrays. It has objects, which are collections of names and values, and arrays, which are objects with a special length property and some handy methods. – RobG Commented May 23, 2011 at 20:56
Add a ment  | 

1 Answer 1

Reset to default 9

It seems you are mixing arrays and objects. Associative arrays should be realized with objects:

myArray = {};
myArray['key'] = value;
myArray['key1'] = value1;

It is a bit confusing though because in your output, the objects don't have key anymore (so it worked), but the array containing those objects as undefined values. I cannot see how delete myArray['key']; is related to your output and which variable now contains which value (please clarify).

But it looks like you did something like:

var container = new Array(6);
container[1] = myArray;
container[3] = myArray;

This will initialize the array with 6 undefined values (sort of) and then set the second and forth value to something else.

If you want to use that "array" as associative array, you should declare it as object too:

var container = {};

Please post more code if you need a better answer.

Update: Yes, you should declare displayedWidgets as object:

var widgets = {
    displayedWidgets: {},

    clear: function() {
        this.displayedWidgets = {};
    },

    add: function(widget) {  
        this.displayedWidgets[widget.id] = widget;
    },

    addArray: function(newWidgets) {
        // note that `each` is only available in newer browsers,
        // just loop over the array
        for(var i = newWidgets.length; i--; ) {
            this.add(newWidgets[i]);
        }
    },

    remove: function(widgetId) {
        if (widgetId in this.displayedWidgets) {
            delete this.displayedWidgets[widgetId];
        }
    }
};

本文标签: Delete entries from an associative array (JavaScript)Stack Overflow