Since this is the nodejs list, I'm going to assume you're working in
node. In that case, Object.keys is built-in and it's really fast in
V8 (much faster than for .. in). Also if you want to share code with
the browser and it's not a mobile app (meaning it needs to support old
ES3 browsers), then it's easy and safe to add an Object.keys function.
It's not on the object prototype.
function forEach(obj, callback, thisp) {
var keys = Object.keys(obj);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (callback.call(thisp, obj[key], key, obj) break;
}
}
Then to use this function, simply do:
forEach(obj, function (value, key) {
// do something
});
If you want to preserve your "this" value, just pass it in as an
argument after the callback. The built-in Array.prototype.forEach
works just like this. If you're comfortable changing
Object.prototype, then put this function there (moving the first
argument to `this`).
If you want to be able to break out of the loop, simply return true
from the callback.
For older browsers, Object.keys can be implemented simply as:
Object.keys = function (obj) {
var key, keys = [];
for (key in obj) {
if (!obj.hasOwnProperty) continue;
keys.push(key);
}
return keys;
};
On Mon, Feb 13, 2012 at 9:01 AM, Axel Kittenberger <[email protected]> wrote:
>> You could do : myobj.hasOwnProperty(key) But then if myobj is NULL it will
>> throw an error.
>
> Unless nil suddendly has keys, it wouldn't go into loop anyway.
>
> Personally I don't like forEach too much, since I cannot break from
> it, when I decide the rest is uninteresting. Or even return from the
> parents function. At least not without some
> oh-so-clever-but-ugly-misuse of exceptions. Why they didn't make the
> standard forEach in that returning false terminates the loop is beyond
> me, or even designed javascript so you can do "named returns", that is
> returning directly the closures parent, which only would work if the
> closure is not exported out of the function anyway. But honestly this
> is all off-topic to Node.js. To Node javascript comes with its quirks
> as it is, and is no language design project.
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en