Aller au contenu principal

forOwn()

forOwn<T>(object, iteratee): void

Iterates over own enumerable string keyed properties of an object.

DEPRECATED

Use Object.keys().forEach() or Object.entries().forEach() directly instead.


Type Parameters

T: T extends object

The type of the object.


Parameters

object: T

The object to iterate over.

iteratee: (value, key, object) => void

The function invoked per iteration.


Returns: void


Since


See Also

Object.keys() - MDN 2.0.0


Also known as

forEachObj (Remeda) · forOwn (Lodash, es-toolkit) · ❌ (Radashi, Ramda, Effect, Modern Dash, Antfu)


Example

// ❌ Deprecated approach
forOwn({ a: 1, b: 2 }, (value, key) => console.log(key, value));

// ✅ Recommended approach
Object.entries({ a: 1, b: 2 }).forEach(([key, value]) => {
console.log(key, value);
});

How it works?

Iterates over own enumerable properties. Deprecated: Use Object.entries() with forEach.

Native Equivalent

// ❌ forOwn(obj, fn)
// ✅ Object.entries(obj).forEach(([k, v]) => fn(v, k, obj))

Use Cases

Iterate object properties 📌

Iterate over own enumerable properties.

Object.entries(obj).forEach(([key, value]) => {
console.log(key, value);
});

Transform values

Process each property value.

const result: Record<string, number> = {};
Object.entries(prices).forEach(([key, value]) => {
result[key] = value * 1.1; // Add 10%
});