2

Hi i have this object :

object {
 key1:[.....],
 key2:[....],
 key3:[.... ]
}

how does i can delete the last object key (key3)?

i would like to be free to delete last object key without knowing anything about that key.

itsme
  • 45,343
  • 90
  • 210
  • 334
  • There is no such thing as the "last" key in an object. Object's don't keep track of the order of their keys. If you need to keep things in order, use an array. It may be most efficient to keep a separate array of just they keys, depending on your use case. – rjmunro Nov 29 '12 at 10:38

4 Answers4

4

Thsi is the ES5-compatible way of doing it:

obj = {a : 1, b : 2, c : 3};

var k = Object.keys(obj);
delete obj[k[k.length-1]];

or shorter:

delete obj[Object.keys(obj)[Object.keys(obj).length-1]];
David Titarenco
  • 30,718
  • 13
  • 54
  • 108
  • 1
    Object properties have no specified order in JavaScript, so this will essentially delete a random property. – Tim Down Sep 26 '11 at 08:42
2

There is not "last" object key within an object in Javascript. Object keys are not ordered and hence, there cannot be first or last.

jAndy
  • 212,463
  • 51
  • 293
  • 348
2

I guess, technically, the keys aren't in any specific order, but anyway...

var key;
for (key in obj);
delete obj[key];

It iterates over the whole object, and then deletes whatever was the last thing to be visited.

edit to illustrate

obj = {a : 1, b : 2, c : 3};

for (key in obj); // loops over the entire object, doing nothing *EXCEPT* 
                  // updating the `key` variable

alert(key); // "c" ... the last value of `key` was 'c'

delete obj[key];  // remove obj.c
nickf
  • 499,078
  • 194
  • 614
  • 709
  • 1
    @jAndy, I've made an edit to illustrate what's happening. It does work. Yes, technically there's no ordering to object, but all JS implementations I've iterate over them in the order they were added, and in any case, there will always be a "last" one to be visited. – nickf Sep 26 '11 at 00:51
  • @jAndy: it does make sense and it does work. +1 for an interesting/smart solution – David Titarenco Sep 26 '11 at 00:52
  • 1
    @nickf, David: someone shows me the spec for that and I'll admit I was wrong. otherwise, this is asking for trouble. – jAndy Sep 26 '11 at 09:03
  • @jAndy: You're right. There was something close to a consensus but browsers are diverging on this now. ECMAScript is specific on the point that property enumeration order is implementation-dependent, so it's always been a bad idea to rely on a specific order; Chrome has led the way in exploiting this for performance gains and other browsers (IE 9, Opera) have followed. See http://code.google.com/p/v8/issues/detail?id=164 – Tim Down Sep 28 '11 at 16:52
2

You can't assume that the last element added will be the last element listed in a javascript object. See this question: Elements order in a "for (… in …)" loop

In short: Use an array if order is important to you.

Community
  • 1
  • 1
Rusty Fausak
  • 6,755
  • 1
  • 24
  • 37
  • The answers in that post specifically stated that arrays can't be trusted either... – worc Jan 27 '12 at 11:45