Table of contents
  1. Filter and rebuild Object
  2. ForEach on Object Keys
  3. ForEach on Entries
  4. If Object Key Exists
  5. Get Object Key By Value
  6. Create New With Default Props
  7. Flatten Nested Objects




Filter and rebuild Object

Object.fromEntries(Object.entries(props).filter(([k, v]) => !customProps.includes(k)),);

ForEach on Object Keys

for (let param of Object.keys(params)) {
    routeUrl = routeUrl.replace(`:${param}`, params[param]);
}

ForEach on Entries

destructure tuple for key value

for (const [key, value] of Object.entries(unionObject)) {
    if (value
            .matchName((enumInfo) => enumInfo.value)
            .replace(/\s/g, "")
            .toLowerCase() === enumValue.replace(/\s/g, "").toLowerCase()) return key;
}

If Object Key Exists

Object.hasOwn(dimensionsObj, key)
? dimensionsObj[key]?.height
: null;

Get Object Key By Value

export const getKeyByValue = (object, value) => {
    return Object.keys(object).find((key) => object[key] === value);
};

Create New With Default Props

const defaultObj = passedDefaultPPTReturnObject
                   ? {...passedDefaultPPTReturnObject}
                   : {};

for (const newProp of newProps) {
    Object.assign(defaultObj, newProp);
}

Flatten Nested Objects

export const flattenMessages = (nestedMessages, prefix = "") => {
    if (nestedMessages === null || nestedMessages === undefined) {
        return {};
    }
    return Object.keys(nestedMessages).reduce((messages, key) => {
        const value = nestedMessages[key];
        const prefixedKey = prefix
                            ? `${prefix}.${key}`
                            : key;

        if (typeof value === "string") {
            Object.assign(messages, {[prefixedKey]: value});
        }
        else {
            Object.assign(messages, flattenMessages(value, prefixedKey));
        }

        return messages;
    }, {});
};