Last active
April 13, 2024 21:03
-
-
Save bisubus/2da8af7e801ffd813fab7ac221aa7afc to your computer and use it in GitHub Desktop.
ES5/ES6/ES2017/ES2019 omit & pick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.keys(obj) | |
.filter((key) => ['blacklisted', 'keys'].indexOf(key) < 0) | |
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.entries(obj) | |
.filter(([key]) => !['blacklisted', 'keys'].includes(key)) | |
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.fromEntries( | |
Object.entries(obj) | |
.filter(([key]) => !['blacklisted', 'keys'].includes(key)) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.keys(obj) | |
.filter((key) => ['whitelisted', 'keys'].indexOf(key) >= 0) | |
.reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.entries(obj) | |
.filter(([key]) => ['whitelisted', 'keys'].includes(key)) | |
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.fromEntries( | |
Object.entries(obj) | |
.filter(([key]) => ['whitelisted', 'keys'].includes(key)) | |
); |
Another omit:
function omit(obj,keys){return keys.reduce((r,key)=>(delete r[key],r),{...obj})}
Benchmark:
http://jsben.ch/TISS9
ES5 omit:
function omit(obj, keys) {
const n = {};
Object.keys(obj).forEach(function(key) {
if (keys.indexOf(key) === -1) {
n[key] = obj[key];
}
});
return n;
}
Not omit nor pick but I found a way to replace _get
:
// falls back to a default value when response.settings is missing or nullish
// (response.settings == null) or when response.settings.animationDuration is missing
// or nullish (response.settings.animationDuration == null)
const animationDuration = _get(response, "settings.animationDuration", 300);
const animationDuration = response.settings?.animationDuration ?? 300;
const buildAnObjectFromAQuery = query => ({
...query.foo && { foo: query.foo },
...query.bar && { bar: query.bar },
});
@kgstew It's good only for cases where only truthy values are useful, e.g. foo and bar are objects. Even better with destructuring:
const buildAnObjectFromAQuery = ({ foo, bar }) => ({
...foo && { foo },
...bar && { bar }
});
Another solution to omit
on a line can be:
const omit = (props, obj) => props.reduce((acc, key) => ((key, {[key]: _, ...rest}) => rest)(key, acc), obj)
Object.assign({}, ...['whitelisted', 'keys'].map(key => ({ [key]: obj[key] })))
This will not work if obj
is an empty object.
Typesafe omit:
export function omit<T extends object, K extends keyof T>(obj: T, paths: K[]): Omit<T, K> {
return {
...paths.reduce((mem, key) => ((k: K, { [k]: ignored, ...rest }) => rest)(key, mem), obj as object),
} as Omit<T, K>;
}
Another solution to pick
const pick = <T extends object, K extends keyof T>(
whitelisted: K[],
target: T,
defaultValue?: any
) =>
Object.fromEntries(
whitelisted.map((key) =>
[ key, key in target ? target[key] : defaultValue ]
)
);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More efficient omit: