@nevware21/ts-utils
    Preparing search index...

    Function forEachOwnKey

    • Calls the provided callbackFn once for each own enumerable key (string and symbol) in the supplied value. The callback can stop iteration early by returning -1.

      This helper works with plain objects, arrays, and functions while safely ignoring null and undefined values.

      Note: Unlike objForEachKey, this helper also iterates enumerable symbol keys via Object.getOwnPropertySymbols. Use forEachOwnKeySafe when iterating untrusted input to filter out unsafe keys like __proto__, constructor, and prototype.

      Type Parameters

      • T

        The source type.

      Parameters

      • theObject: T

        The object-like value to iterate.

      • callbackfn: (key: PropertyKey, value: T[keyof T]) => number | void

        Invoked for each own enumerable key.

      • OptionalthisArg: any

        [Optional] The this context for the callback. If omitted, null, or undefined, the iterated object is used as this.

      Returns void

      0.14.0

      // Iterates both string and symbol keys
      const sym = Symbol("id");
      const obj = { name: "Alice", [sym]: 42 };

      forEachOwnKey(obj, (key, value) => {
      console.log(String(key), value);
      // "name" "Alice"
      // "Symbol(id)" 42
      });
      // Stop iteration early by returning -1
      const obj = { a: 1, b: 2, c: 3 };
      forEachOwnKey(obj, (key) => {
      console.log(key); // "a", "b"
      if (key === "b") {
      return -1; // stops here
      }
      });
      // Custom this context
      const obj = { value: 10 };
      const ctx = { multiplier: 3 };
      forEachOwnKey(obj, function(key, val) {
      console.log((val as number) * this.multiplier); // 30
      }, ctx);