apphouse
Version:
Component library for React that uses observable state management and theme-able components.
51 lines (48 loc) • 1.22 kB
text/typescript
/**
* creates a new object (newObj) by copying all the properties from the original
* object using the spread operator ({ ...obj }). Then, it iterates over each
* key in keysToOmit using forEach, and deletes those keys from the newObj
* using the delete keyword.
* @param obj the original object
* @param keysToOmit the keys to omit
* @returns a new object (newObj) that includes all properties of the original object
* except the ones specified in keysToOmit.
*
*
* @example
* For example, let's say you have an object like this:
*
* ```javascript
* const originalObject = {
* name: 'John',
* age: 25,
* city: 'New York',
* country: 'USA'
* };
* ```
*
* To omit the `name` and `age` keys, you can use the `omit` function like this:
*
* ```javascript
* const newObj = omit(originalObject, ['name', 'age']);
*
* console.log(newObj);
* ```
*
* The output will be:
*
* ```javascript
* { city: 'New York', country: 'USA' }
* ```
*/
export function omit<T>(obj: T, keysToOmit: string[]) {
const newObj = { ...obj };
if (!keysToOmit.length) {
return newObj;
}
keysToOmit.forEach((key) => {
const k = key as keyof T;
delete newObj[k];
});
return newObj;
}