[jQuery] Re: How to iterate and manipulate an object

2007-10-13 Thread Michael Geary
Hector, I'm afraid you lost me there. I'm always eager to improve my code, but I am having trouble figuring out what you're talking about. I *think* you were saying that the code promotes some bad coding habit having to do with loop iteration, but I'm not sure what that could be. The only loop in

[jQuery] Re: How to iterate and manipulate an object

2007-10-12 Thread Michael Geary
From: Michael Geary Just for fun, here's a primitive little cache implementation that lets you add/remove/get items and also prune items from the beginning of the list... And if anyone tries to use that code for anything, beware! It has a serious bug. (Do you see what it is?) If anyone

[jQuery] Re: How to iterate and manipulate an object

2007-10-12 Thread Pops
Hi Mike, I didn't analyze your code, but as I said (or maybe I was thinking of saying it but do not) is that JavaScript, to me, a guy is extremely strict and high software quality development practices, promotes bad coding habits. I say that because I have already caught myself doing stuff that

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Wizzud
It's the only sensible way. jQuery provides jQuery.each( obj/arr, function() ) to iterate over arrays/objects, which boils down to using the for...in construct. To remove properties from an object ... eg. delete obj.x2; On Oct 11, 8:57 am, Pops [EMAIL PROTECTED] wrote: Is this the only way to

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Wizzud
@Michael Snap! On Oct 11, 9: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

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Michael Geary
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

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Pops
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

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Flesler
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

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread d . wachss
I would be very wary of assuming that the for loop goes Last In First Out. To quote the Mozilla docs (http://developer.mozilla.org/en/docs/ Core_JavaScript_1.5_Reference:Statements:for...in): A for...in loop iterates over the properties of an object in an arbitrary order. It may work correctly

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Pops
Thanks Danny. If associated arrays in javascript is using memory map in the RTE, e.g., C/C++ string list map, then the key is a hash and there is no order to it. Traversal is unpredictable and FIFO (queues), LIFO (stacks) does not apply here. -- HLS On Oct 11, 6:58 pm, [EMAIL PROTECTED]

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Flesler
You can just test it in every browser you are interested and see what's the result, that is always done when trying to overcome browser incompatibilities, no magic solution for everything when dealing with browsers. On 11 oct, 20:18, Pops [EMAIL PROTECTED] wrote: Thanks Danny. If associated

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Flesler
And indeed, I tested this in FF, worked well... then (surprisingly?) tested it in IE, and it worked oddly. this was the code: var o = { b:1, c:2, d:3 }; delete o.b; o.e = 4; o.b = 2; o.a = 5; for(i in o)alert(i); IE would put o.b back into it's place (1st).. so yes, if

[jQuery] Re: How to iterate and manipulate an object

2007-10-11 Thread Michael Geary
You can delete properties of an object while you're running a for loop over that object. No special tricks are required. If you delete a property that's already been iterated (including the current iteration), nothing special happens - the iteration continues on its merry way. If you delete a