re1man
re1man

Reputation: 2367

splicing specific value in array of array

I have an associative array stored in another associative array. I know how to splice a specific value in just a regular array as such:

arr.splice(arr.indexOf('specific'), 1);

I was wondering how one could splice an array such as this:

arr['hello']['world']

EDIT Would this shorten hello['world']['continent']

var hello = {};
            hello['world'] = {};
            hello['world']['continent'] = "country";
            delete hello['world']['continent'];
            alert(hello['world']['continent'])

Upvotes: 0

Views: 282

Answers (3)

GoldenNewby
GoldenNewby

Reputation: 4452

You should be able to just just use the delete keyword.

delete arr["hello"]["world"]

How do I remove objects from a javascript associative array?

Edit, based on other comments:

For the sake of readability, you can also do:

delete arr.hello.world

Since we are really just talking about objects, and not traditional arrays, there is no array length. You can however delete a key from an object.

Upvotes: 2

max
max

Reputation: 102036

JavaScript does not have associative arrays.

Use objects:

var x = {
  a : 1,
  b : 2,
  c : {
     a : []
  }
}

delete x.c.a;

Upvotes: 1

bhamlin
bhamlin

Reputation: 5187

An "associative array" in javascript isn't actually an array, it's just an object that has properties set on it. Foo["Bar"] is the same thing as Foo.Bar. So you can't really talk about slicing or length here.

Upvotes: 0

Related Questions