omit

Returns a new object with the specified keys omitted.

1.
/**
2.
* Returns a new object with the specified keys omitted.
3.
**/
4.
const omit = <T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> => {
5.
const newObj = { ...obj };
6.
keys.forEach((key) => delete newObj[key]);
7.
return newObj as Omit<T, K>;
8.
};
9.
10.
export default omit;

1. Installtion

npx @jrtilak/lazykit add omit

2. Parameters

  • obj (T)
    The input object from which properties will be omitted.

  • keys (K[])
    An array of keys to omit from the input object. The keys must be valid keys of the input object.

3. Returns

  • Omit<T, K>
    A new object that contains all properties of the input object except for the specified keys.

4. Example Usage

1.
import omit from "@/helpers/omit";
2.
3.
const originalObject = {
4.
name: "Alice",
5.
age: 28,
6.
city: "Paris",
7.
occupation: "Engineer",
8.
};
9.
10.
const omittedObject = omit(originalObject, ["age", "occupation"]);
11.
12.
console.log(omittedObject);
13.
// Output: { name: "Alice", city: "Paris" }
1.
import omit from "@/helpers/omit";
2.
3.
const settings = {
4.
theme: "dark",
5.
notifications: true,
6.
language: "en",
7.
fontSize: 16,
8.
preferredColor: {
9.
primary: "blue",
10.
secondary: "green",
11.
}
12.
};
13.
14.
const updatedSettings = omit(settings, ["theme", "fontSize", ]);
15.
16.
console.log(updatedSettings);
17.
// Output: { notifications: true, language: "en" }