forOwn
object
Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property.
Installation
Import
import { forOwn } from '@tulx/utils';Source Code
Implementation
/**
* Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property.
*
* @param object - The object to iterate over.
* @param iteratee - The function invoked per iteration.
* @returns Returns object.
*
* @example
* ```ts
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
* Foo.prototype.c = 3;
* forOwn(new Foo(), (value, key) => console.log(key)); // Logs 'a' then 'b'
* ```
*/
export function forOwn<T extends Record<string, unknown>>(
object: T,
iteratee: (value: unknown, key: string, object: T) => void
): T {
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
iteratee(object[key], key, object);
}
}
return object;
}
Example
import { forOwn } from '@tulx/utils';
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
forOwn(new Foo(), (value, key) => console.log(key)); // Logs 'a' then 'b'Related Functions
assign
Assigns own enumerable string keyed properties of source objects to the destination object.
assignIn
This method is like assign except that it iterates over own and inherited source properties.
assignInWith
This method is like assignIn except that it accepts customizer which is invoked to produce the assigned values.
assignWith
This method is like assign except that it accepts customizer which is invoked to produce the assigned values.
at
Creates an array of values corresponding to paths of object.
create
Creates an object that inherits from the prototype object.