The object type
The object to iterate over
A function that accepts up to two arguments, the key name and the current value of the property represented by the key.
OptionalthisArg: any
[Optional] An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, null or undefined the object will be used as the this value.
// Basic iteration - only string keys
const obj = { name: "Alice", age: 30 };
objForEachKey(obj, (key, value) => {
console.log(key, value); // name Alice, age 30
});
// Unsafe keys warning - use objForEachKeySafe for untrusted input
// Note: use Object.defineProperty (not an object literal) so "__proto__" is
// created as a normal own enumerable property rather than being treated
// specially by the JS engine.
const untrustedObj: any = { name: "Bob" };
Object.defineProperty(untrustedObj, "__proto__", { value: "attack", enumerable: true, configurable: true, writable: true });
objForEachKey(untrustedObj, (key, value) => {
console.log(key, value); // name Bob, __proto__ attack (UNSAFE!)
});
// Safe iteration with objForEachKeySafe
objForEachKeySafe(untrustedObj, (key, value) => {
console.log(key, value); // name Bob (unsafe keys filtered)
});
// A throwing getter does not stop the iteration
const obj = { a: 1, get b(): number { throw new Error("fail"); }, c: 3 };
objForEachKey(obj, (key, value) => {
console.log(key, value); // a 1, c 3 (b is skipped)
});
Calls the provided
callbackFnfunction once for each key in an object. This is equivalent toarrForEach(Object.keys(theObject), callbackFn)or if not using the array helperObject.keys(theObject).forEach(callbackFn)except that this helper avoids creating a temporary array of the object keys before iterating over them and like thearrForEachhelper you CAN stop or break the iteration by returning -1 from thecallbackFnfunction. As the keys are not read first, a key which is deleted by thecallbackFnbefore it is reached is not visited (rather than being visited with anundefinedvalue), and (like for...in) whether a key added by thecallbackFnis visited depends on the runtime.Note: This helper only iterates string (and numeric) keys (via for...in loop) and does NOT include enumerable symbol keys. If you need to iterate all keys including symbol, use forEachOwnKey or forEachOwnKeySafe instead.
If reading a property value throws (for example a getter that throws an exception), the exception is caught and the key is skipped (the
callbackFnis not called for it), so a single throwing getter does not stop the iteration of the remaining keys, and a helper which copies the values (eg. objExtend or objDeepCopy) doesn't copyundefinedfor it (replacing an existing value). Since 0.20.0, previously the exception stopped the iteration.Caution: this helper does not filter unsafe keys like
__proto__,constructor, orprototype. If your callback uses the returned key to assign directly to another object (for exampletarget[key] = value), validate keys first (for example with isUnsafePropKey) or use objForEachKeySafe when iterating untrusted input.