How to iterate over every property of an object in javascript using Prototype?

You have to first convert your object literal to a Prototype Hash : Store your object literal var obj = {foo: 1, bar: 2, barobj: {75: true, 76: false, 85: true}} // Iterate like so. The $H() construct creates a prototype-extended Hash. $H(obj).

Each(function(pair){ alert(pair. Key); alert(pair. Value); }).

You have to first convert your object literal to a Prototype Hash: // Store your object literal var obj = {foo: 1, bar: 2, barobj: {75: true, 76: false, 85: true}} // Iterate like so. The $H() construct creates a prototype-extended Hash. $H(obj).

Each(function(pair){ alert(pair. Key); alert(pair. Value); }).

Perfect! This is exactly what I was looking for. – OverloadUT Feb 25 '09 at 21:38 22 Unfortunately since a similar question was closed, one that just wanted to iterate over a simple javascript object without Prototype, I now have to treat this answer as if it were the same as the question that was closed due to "duplication."

So, horrible example as it forces the user to load Prototype. The user didn't say anything about Prototype so forcing them to load an unwanted library is not useful. (remember, treating this as if it were really a duplicate).

If the other question had not been closed due to the false claim of duplication I would not have to down vote the answer. – Mike Bethany Feb 12 '10 at 21:05 Didn't the asker mention they wanted Prototype (or was the question edited? )?

Anyway it's all good – emurano Jun 15 at 4:52 Great! +1 for pointing towards Prototype. – Sander Jun 16 at 8:45.

There's no need for Prototype here: JavaScript has for..in loops. If you're not sure that no one messed with Object. Prototype, check hasOwnProperty() as well, ie for(var prop in obj) { if(obj.

HasOwnProperty(prop)) doSomethingWith(objprop); }.

61 Thank you for the actual answer without forcing us to load an unwanted library. – Mike Bethany Feb 12 '10 at 21:06 2 this not the rigth answer. The questions states that prototype has to be used!

Freedom of choice - haha... – Sven Larson Mar 21 '10 at 21:20 3 +1 It's the right answer to the title of the question. – Daniel Earwicker Jul 24 '10 at 11:57 The original title of this question mentioned Prototype, which I have added back in for clarity. This question, as evidenced by the very first line of the body, was specifically asking about an environment in which Prototype is in use.

– OverloadUT Apr 15 at 18:17 I don't think this for loop works in ie7 – Purrell Jun 6 at 4:43.

Perhaps the function provided at this thread is useful if trying to find specific values in an object, no matter how deeply nested it is.

You should iterate over the keys and get the values using square brackets. See: How do I enumerate the properties of a javascript object? EDIT: Obviously, this makes the question a duplicate.

That method is strongly discouraged against in the Prototype docs: prototypejs. Org/api/array – OverloadUT Feb 25 '09 at 21:38 Also, I don't think this is a duplicate because I was looking for a Prototype-native solution which is what I got. The other question is decent for someone who doesn't want it to use a framework, but this solution is much safer if you're using Prototype.

– OverloadUT Feb 25 '09 at 21:42 @OverloadUT: you didn't read carefully enough: it's discouraged to iterate over the properties of arrays, not plain objects – Christoph Feb 25 '09 at 22:54.

Var obj = {name:"Jack Bauer", username:"JackB", id:12345, agency:"CTU", region:"Los Angeles"}; try { while (pair = Iterator(obj).next()) { console. Log("key:" + pair0 + ", value:" + pair1 + "\n"); } } catch (err if err instanceof StopIteration) { console. Log("End of record.

\n"); } catch (err) { console. Log("Unknown error: " + err. Description + "\n"); } As of Javascript 1.7.

Over every property of an object in javascript is itered (without quotes):.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions