@web3r/flowerkit
Version:
Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).
34 lines (33 loc) • 1.11 kB
text/typescript
export type TGetAllSamePropsFromObjArgs = Parameters<typeof getAllSamePropsFromObj>;
export type TGetAllSamePropsFromObjReturn = ReturnType<typeof getAllSamePropsFromObj>;
/**
* Gets all values inside an object by the specified key, including deeply nested objects
* @template T
* @param {Record<string, unknown>} obj Source object (can be nested)
* @param {string} prop Property name to collect values for
* @returns {T[]} Collected values
* @throws {TypeError} getAllSamePropsFromObj: prop must be a non-empty string
* @throws {TypeError} getAllSamePropsFromObj: obj must be an object
* @example
* // How to get all duplicate key values inside an object?
* const myObj = {
* someProp1: {
* a: "value 1",
* b: 2,
* c: 3,
* d: {
* a: 1,
* b: 2
* }
* },
* someProp2: {
* a: "value 3",
* b: 2,
* c: {
* a: "value 4"
* }
* }
* }
* getAllSamePropsFromObj<string | number>(myObj, "a") // [ "value 1", 1, "value 3", "value 4" ]
*/
export declare const getAllSamePropsFromObj: <T = unknown>(obj: unknown, prop: string) => T[];