最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove all elements from hash and keep the reference - Stack Overflow

programmeradmin4浏览0评论

I need to remove everything from a hash/object and keep the reference. Here is an example

var x = { items: { a: 1, b: 2} }

removeItems(x.items) ;
console.log(x.items.clean) ;

function removeItems(items) {
    var i ;
    for( i in items; i++ ) {
       delete items[i] ;
    }

    items.clean = true ;
}

I was wondering if there is a shorter way to achieve this. For example, cleaning an array can be done as follows

myArray.length = 0 ;

Any suggestions?

I need to remove everything from a hash/object and keep the reference. Here is an example

var x = { items: { a: 1, b: 2} }

removeItems(x.items) ;
console.log(x.items.clean) ;

function removeItems(items) {
    var i ;
    for( i in items; i++ ) {
       delete items[i] ;
    }

    items.clean = true ;
}

I was wondering if there is a shorter way to achieve this. For example, cleaning an array can be done as follows

myArray.length = 0 ;

Any suggestions?

Share Improve this question edited Oct 22, 2014 at 16:27 Unihedron 11k13 gold badges63 silver badges72 bronze badges asked Mar 23, 2013 at 20:47 Jeanluca ScaljeriJeanluca Scaljeri 29.2k66 gold badges235 silver badges382 bronze badges 6
  • The i++ in your code is useless. – Denys Séguret Commented Mar 23, 2013 at 20:50
  • I don't think there is a shortcut for that. – Denys Séguret Commented Mar 23, 2013 at 20:57
  • 3 why do you want to "keep the reference"? – Florian Margaine Commented Mar 23, 2013 at 20:57
  • 1 and what on earth does that even mean? – Alnitak Commented Mar 23, 2013 at 20:57
  • 1 @Alnitak I supposed (but I might be wrong) that OP passes a reference to x.items elsewhere and doesn't want to change the object, just empty it. But there probably is a better solution to his problem, like passing an enclosing object. – Denys Séguret Commented Mar 23, 2013 at 21:00
 |  Show 1 more ment

3 Answers 3

Reset to default 8

There is no easy way to do this at the moment, however the ECMAScript mittee sees this need and it is in the current specification for the next version of JS.

Here is an alternative solution, using ECMAScript 6 maps:

var x = {}
x.items = new Map();
x.items.set("a",1);
x.items.set("b",2);

//when you want to remove all the items

x.items.clear();

Here is a shim for it so you can use it in current-day browsers.

This does not work:

var i ;
for( i in items; i++; ) {
   delete items[i] ;
}

It creates a for-loop with the init code i in items (which btw evaluates to false as there is no "undefined" key in items, but that doesn't matter), and the condition i++ and no update code. Yet i++ evaluates to the falsy NaN, so your loop will immediately break. And without the second semicolon, it even as a SyntaxError.

Instead, you want a for-in-loop:

for (var i in items) {
    delete items[i];
}

Btw, items.clean = true; would create a new property again so the object won't really be "clean" :-)

I was wondering if there is a shorter way to achieve this. For example, cleaning an array can be done as follows

No. You have to loop all properties and delete them.

There isn't a shorter way, sorry. Your loop shouldn't have the i++ though.

function removeItems(items) {
  for(var i in items) {
    delete items[i];
  }

  items.clean = true;
}

Restructuring your code and just doing x.items = {} would be better though.

发布评论

评论列表(0)

  1. 暂无评论