0

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'])
3
  • JavaScript doesn't have "associative" arrays. Commented Mar 7, 2012 at 19:46
  • What are hello and world? What does arr look like? What do you want it to look like? Commented Mar 7, 2012 at 19:48
  • arr['hello']['world'] can also be accessed as arr.hello.world Commented Mar 7, 2012 at 20:06

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

would this shorten the array though?
It would remove the world object all together. So, it would shorten arr["hello"]
wouldn't it just make it undefined...im pretty sure
I was thinking of the length more in the context of listing the keys of the object. In that case I believe it would shorten the list of keys.
@Rocket The reason I put "shortens" in those quotes is exactly what you're suggesting... "shortens" doesn't make sense (exactly as you said). That's what I was trying to communicate. :)
|
1

JavaScript does not have associative arrays.

Use objects:

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

delete x.c.a;

Comments

0

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.

Comments

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.