90

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!

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
Allie the Icon
  • 2,142
  • 2
  • 18
  • 26

3 Answers3

555

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
  • 164,997
  • 36
  • 182
  • 240
  • 141
    Thank you for the actual answer without forcing us to load an unwanted library. –  Feb 12 '10 at 21:06
  • 8
    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
  • 1
    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. – Allie the Icon Apr 15 '11 at 18:17
  • 1
    The more you write Javascript, the more you'll hate writing out `for` loops :) – Kenan Banks Aug 18 '11 at 06:22
  • 13
    Unfortunately this is the question that pops up as #1 if you Google for something like "how to iterate over every object property javascript", so a lot of people coming here are indeed probably looking for the answer to this question. The question they want is this one: http://stackoverflow.com/questions/921789/how-to-loop-through-javascript-object-literal-with-objects-as-members, which really has nothing to do with object literals. – Baxissimo Sep 01 '11 at 19:32
  • I am afraid the `for (var in obj)` iteration doesn't work in IE. – jmishra Feb 13 '12 at 03:22
  • You can extend the object API to make life easy. Although some people might argue that this is a bit risky which I agree: `Object.prototype.each = Object.prototype.each || function(cb) {` `for (var key in this) {` `if (this.hasOwnProperty(key)) {` `cb(this[key], key);` `}` `}` `};' – Ardi Feb 11 '16 at 15:11
42

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);
});
Cameron Shaw
  • 142
  • 11
Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
  • 57
    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. –  Feb 12 '10 at 21:05
  • 2
    Didn't the asker mention they wanted Prototype (or was the question edited?)? Anyway it's all good – emurano Jun 15 '11 at 04:52
  • No need to load an external library – Healkiss Jan 13 '15 at 09:34
0

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.

Community
  • 1
  • 1
Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
  • That method is strongly discouraged against in the Prototype docs: http://www.prototypejs.org/api/array – Allie the Icon Feb 25 '09 at 21:38
  • 1
    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. – Allie the Icon Feb 25 '09 at 21:42
  • 1
    @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