views:

8243

answers:

5

Is there a way to iterate over every property of an object using the Prototype JavaScript framework?

Here's the situation: I am getting an AJAX response in JSON that looks something like this:

{foo: 1, bar: 2, barobj: {75: true, 76: false, 85: true}}

If I evaluate that json response in to a variable response, I want to be able to iterate over each property in the response.barobj object to see which indexes are true and which are false.

Prototype has both Object.keys() and Object.values() but oddly seems to not have a simple Object.each() function! I could take the results of Object.keys() and Object.values() and cross-reference the other as I iterate through one, but that is such a hack that I am sure there is a proper way to do it!

A: 

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.

Can Berk Güder
That method is strongly discouraged against in the Prototype docs:http://www.prototypejs.org/api/array
OverloadUT
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
@OverloadUT: you didn't read carefully enough: it's discouraged to iterate over the properties of arrays, not plain objects
Christoph
+5  A: 

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);
});
Triptych
Perfect! This is exactly what I was looking for.
OverloadUT
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
@pickle pumper. That was completely ridiculous, but do with yourvotes what you will.
Triptych
+42  A: 

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(obj[prop]);
}
Christoph
Thank you for the actual answer without forcing us to load an unwanted library.
Mike Bethany
this not the rigth answer. the questions states that prototype has to be used! freedom of choice - haha...
Sven Larson
+1 It's the right answer to the title of the question.
Daniel Earwicker
A: 

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

snz3
A: 

var obj = {name:"Jack Bauer", username:"JackB", id:12345, agency:"CTU", region:"Los Angeles"}; try { while (pair = Iterator(obj).next()) { console.log("key:" + pair[0] + ", value:" + pair[1] + "\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

Kristian Mandrup