6

I have following object:

{"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}}

In this example I want to remove

"1":{"cid":"1","uid":"3"}

from it. I have tried all solutions that I found on Stack Overflow, and could not make any of them work. I am mostly PHP person, so I might miss something important here?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Milos911
  • 433
  • 3
  • 9
  • 21

6 Answers6

14

Just use delete with the appropriate property.

var obj = {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}};

delete obj["1"];

Note the " around the 1 to mark it as an identifier and not as an array index!

EDIT As pointed out in the comment, obj is an object and no array no matter how you address the [1] property. My last note was just to make it clear that you are working with an object and not an array.

In my experience most people associate integer properties with arrays and string properties with objects. So I thought it might be more helpful to highlight the property in the way given.

Christoph
  • 50,121
  • 21
  • 99
  • 128
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • 4
    It's not an array index because *obj* is an object, not because the value is a string. An early step in resolving property names is to convert them to strings, so the outcome of `obj[1]` and `obj['1']` are indistinguishable in the same way `obj[(function(){return 1;}())]` would be. And since Arrays are Objects, `array[1]` is identical in outcome to `array['1']`. – RobG Apr 16 '12 at 13:20
4
 var myObj= {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}}

delete myObj['1'];

alert ( myObj['1']);

please notice there are Cross platform problems with delete :

Cross-browser issues

Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses delete on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property value is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its old position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    using dot notation on numeric indices gives syntax errors, use brackets! – Elias Van Ootegem Apr 16 '12 at 13:12
  • 1
    @EliasVanOotegem ive already corrected it while you were writing this msg – Royi Namir Apr 16 '12 at 13:13
  • It looks like you're quoting MDN. Probably a good idea to credit the source. :) –  Apr 16 '12 at 13:31
  • @amnotiam Ive put it in quote tags ...and it takes 1 seconds to search it in the web to get to mdn.... – Royi Namir Apr 16 '12 at 13:32
  • @amnotiam I dont wanna be a child , but i saw cpule of your answers which had a quotes without any link or credit.... but i wont post them. we (including you) dont have to be "sharp right " all the time.... – Royi Namir Apr 16 '12 at 13:41
  • If that's the case, I would *appreciate* it if someone gave me a reminder. It baffles me that you would be so apparently offended by a friendly reminder. –  Apr 16 '12 at 14:03
  • @amnotiam I really appreciate you suggestion.... you gave me some good answers back then in jQuery....:) – Royi Namir Apr 16 '12 at 14:17
2
var arr=[[10,20,30],[40,50,60],[70,80,90]];

for(var i=0;i<arr.length;i++){
    for(var j=0;j<arr[i].length;j++){
        if(arr[i][j]==50){
            arr[i].splice(j,1);
        }
    }
}

document.write(arr);
Pang
  • 9,564
  • 146
  • 81
  • 122
Rakesh
  • 21
  • 1
1

JavaScript doesn't have multi dimensional Arrays, only arrays and objects. That said: delete theObject['1']; should work just fine

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 1
    javascript does have multi-dimensional arrays. – jbabey Apr 16 '12 at 13:29
  • @jbabey: granted, if all keys are numeric the var will behave as an array, but in this example: if you assign values step by step (`var theArray = []; theArray['1'] = []; theArray['1']['cid']='2';`), try doing something like `theArray['1'].length`, or try `theArray.length`. These methods return `0` and `2` respectively. It's best to approach the var as an object. – Elias Van Ootegem Apr 16 '12 at 13:46
  • i realize the OP is not using arrays, but that does not mean that javascript multi-dimensional arrays cease to exist :P – jbabey Apr 16 '12 at 14:06
  • fair enough :)... just read your comment under the question itself. It's just one of those common misconceptions, questions starting with 'I have this multi-dimensional Array' followed by an object... it's just something that gets on my nerves, but than I guess I am a bit pedantic. – Elias Van Ootegem Apr 16 '12 at 14:14
1

You could use delete in javascript Ex:

var x = {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}};

delete x['1'];

Also, check this:

Deleting Objects in JavaScript

Community
  • 1
  • 1
Vimalnath
  • 6,373
  • 2
  • 26
  • 47
0

Assign your Object (not an Array) to a variable, this way:

var o = {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}};

Then do:

delete o['1'];

That's it!

Kappei
  • 714
  • 2
  • 15
  • 34