Darren
Darren

Reputation: 13128

Removing JS array element

I've currently got a set of javascript functions that are used to set intervals and clear them. The issue is; the intervals are stored in an array (intervals[])

config: {
    settings: {
        timezone: 'Australia/Perth,',
        base_url: location.protocol + '//' + location.hostname + '' + ((location.pathname) ? location.pathname : ''),
        api_url: '/api/'
    },
    intervals: []
}

There are two functions that work with that array: The first being the interval setter:

interval_set: function(time, name, callback) {
        this.config.intervals[name] = setInterval(callback, time);
    }

and the second being the interval clearer

interval_clear: function(name) {
        if (this.config.intervals[name]) {
            clearInterval(this.config.intervals[name]);
            this.debug('Interval: "' + name + '" cleared.', 1);
        } else {
            this.debug('No interval: "' + name + '" found.', 1);
        }
    }

Now the issue is I need to remove said interval from the intervals array, although as you can see - it isn't set with a specific key.

The interval array looks like this:

Array[0]
    derp: 1

I've tried using splice() but that doesn't seem to work. What would you suggest I do? Should I store it in the array with an index?

Thanks :)

Upvotes: 0

Views: 120

Answers (1)

Russell Zahniser
Russell Zahniser

Reputation: 16354

It sounds like what you want is just:

delete this.config.intervals[name];

Incidentally, you aren't really using intervals as an Array here, you're just using it like a regular Object. So it would be more correct for its starting value to be {} instead of [].

Upvotes: 2

Related Questions