Table of contents
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;
}, {});
};