2

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().

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. Commented May 23, 2011 at 20:56

1 Answer 1

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];
        }
    }
};
Sign up to request clarification or add additional context in comments.

4 Comments

The 'loop over the array' makes problems... How to get an entry of displayedWidgets by index, not by key? The key is the id of the widget what comes from the database.. so key != index.
+1: In JS, unlike PHP, arrays are not to be used as a hash map. The solution suggested is the best thing to do, displayedWidgets should be an object (which is a hash map), not an array which is to be used only when you need to index items by an interger, not a string.
@FFraenz: You cannot have both (with one data structure). If you also need some kind of order of the widgets, then you need another array, where you e.g. add the key. Or you base everything on indices only. Or I don't understand the problem... what do you need the index for?
I think I have to study a bit these things. Thank you for your fast help!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.