forInRight
object
This method is like forIn except that it iterates over properties of object in the opposite order.
Installation
Import
import { forInRight } from '@tulx/utils';Source Code
Implementation
/**
* This method is like forIn except that it iterates over properties of object in the opposite order.
*
* @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;
* forInRight(new Foo(), (value, key) => console.log(key)); // Logs 'c', 'b', then 'a'
* ```
*/
export function forInRight<T extends Record<string, unknown>>(
object: T,
iteratee: (value: unknown, key: string, object: T) => void
): T {
const keys: string[] = [];
for (const key in object) {
keys.push(key);
}
for (let i = keys.length - 1; i >= 0; i--) {
const key = keys[i];
iteratee(object[key], key, object);
}
return object;
}
Example
import { forInRight } from '@tulx/utils';
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
forInRight(new Foo(), (value, key) => console.log(key)); // Logs 'c', 'b', then 'a'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.