How to remove all elements from hash and keep the reference

javascript

Solution

There is no easy way to do this at the moment, however the ECMAScript committee 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.

Problem

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?

Original source