The for iteration goes from the older ones to newer ones, so if you
want to keep newer ones (FIFO):
function pruneCache(amt) {
   for (var i in cache) {
        if (cache.length <= amt) return;//pruned
        delete cache[i];
        cache.length--;
   }
}
that asumes you are keeping the length updated till here. If you want
to remove newer first(LIFO):
function pruneCache(amt) {
   delete cache.length;//remove length to avoid iterating it
   var copy = {length:0}; //new hash
   for (var i in cache) {
        if (copy.length >= amt) break;//done
        copy[i] = cache[i];
        copy.length++;
   }
   cache = copy;//replace the old cache
}

The other option is to have 2 parallel arrays of keys and values...
Hope that helps.

Ariel Flesler

On Oct 11, 4:54 pm, Pops <[EMAIL PROTECTED]> wrote:
> Thanks Mike and Wizzud.
>
> Question:
>
> What I wanted to use this for was my cache and to "truncate" old
> data.  I see that using a real array will allow me to use the
> inherited .length property to set a new size and it will do
> truncation.  But since I am using an associated array, the length
> property is no longer usable.
>
> So if I use the for loop with delete, will I be pulling the "rug from
> under its feet?"
>
> Typically, in code designs like this, you would do a reverse traversal
> to delete the last entries first so you can keep with the internal
> loop counters and/or references.
>
> In other words, is this "safe?"
>
>   function pruneCache(amt) {
>     var n = 0;
>     for (var i in cache) {
>         if (amt > 0) {
>            amt--;
>            delete cache[i];
>            return;
>         }
>         n++;
>     }
>     cache.length = n;
>   }
>
> If not, then in lieu of a reverse loop syntax,  I would probably need
> to copy the cache first?   I have not checked but if returning false
> stops the traveral that would be more efficient.
>
> I could recode all this into a pure indexed array and then use
> the .length, but then I would need a fast lookup method or 2nd matrix
> to map the associated name with the array index.
>
> Thanks
>
> --
> HLS
>
> On Oct 11, 4:26 am, "Michael Geary" <[EMAIL PROTECTED]> wrote:
>
>
>
> > > From: Pops
>
> > > Is this the only way to iterate through an object?
>
> > >      for ( x in myObject) {
> > >      }
>
> > Ultimately, yes. You can use something like $.each() on an object, but it
> > just runs that same for loop internally - see the source code for $.each().
>
> > > and how can you remove an field in an object?
>
> > > For example:
>
> > >    var obj = {
> > >        x1: 1123,
> > >        x2: 212,
> > >        x3: 123131
> > >    };
>
> > > I want to remove obj.x2 the obj object to end up with:
>
> > >    { x1: 1123, x3: 123131}
>
> > delete obj.x2;
>
> > or
>
> > delete obj['x2'];
>
> > Note that it is not an error if the property doesn't exist, so you don't
> > have to "protect" the delete with code like this:
>
> > if( 'x2' in obj ) delete obj.x2;  // unnecessary precaution
>
> > -Mike- Hide quoted text -
>
> - Show quoted text -

Reply via email to