admin管理员组文章数量:1356702
As reference Remove blank attributes from an Object in Javascript, how to make it Typescript patible?
JS function (nested objects | ES10):
function removeEmpty(obj) {
return Object.fromEntries(
Object.entries(obj)
.filter(([_, v]) => v != null)
.map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
);
}
I've tried with Exclude<T, null>
but it doesn't works with nested objects and I think it's not the correct utility to use.
Note that the returned type should remove null
type(s) but keeps undefined
.
Example/Expected behaviour:
type TestType = {
a?: {
b?: {
c: string;
} | null;
z?: {
x: string;
};
} | null;
};
const testObj: TestType = {
a: {
b: null,
z: { x: 'Hi' }
}
};
const resultObj = removeEmpty(testObj);
Where resultObj
type is similar to:
type ResultTestTypeExample = {
a?: {
b?: {
c: string;
};
z?: {
x: string;
};
};
};
As reference Remove blank attributes from an Object in Javascript, how to make it Typescript patible?
JS function (nested objects | ES10):
function removeEmpty(obj) {
return Object.fromEntries(
Object.entries(obj)
.filter(([_, v]) => v != null)
.map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
);
}
I've tried with Exclude<T, null>
but it doesn't works with nested objects and I think it's not the correct utility to use.
Note that the returned type should remove null
type(s) but keeps undefined
.
Example/Expected behaviour:
type TestType = {
a?: {
b?: {
c: string;
} | null;
z?: {
x: string;
};
} | null;
};
const testObj: TestType = {
a: {
b: null,
z: { x: 'Hi' }
}
};
const resultObj = removeEmpty(testObj);
Where resultObj
type is similar to:
type ResultTestTypeExample = {
a?: {
b?: {
c: string;
};
z?: {
x: string;
};
};
};
Share
Improve this question
edited May 11, 2022 at 16:03
Carlo Corradini
asked May 11, 2022 at 15:55
Carlo CorradiniCarlo Corradini
3,4553 gold badges22 silver badges28 bronze badges
2 Answers
Reset to default 6Does this work for you?
type ExpandRecursively<T> = T extends object
? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
: T;
type RemoveNull<T> = ExpandRecursively<{ [K in keyof T]: Exclude<RemoveNull<T[K]>, null> }>
Usage:
function removeEmpty<T extends object>(obj: T): RemoveNull<T> {
return Object.fromEntries(
Object.entries(obj)
.filter(([_, v]) => v != null)
.map(([k, v]) => [k, v === Object(v) ? removeEmpty(v) : v])
) as RemoveNull<T>
}
Playground
export function removeNull<T extends object>(obj: T | any) {
Object.keys(obj).forEach(k => {
const key = k as keyof T
if (obj[key] && typeof obj[key] === 'object') removeNull(obj[key])
else if (obj[key] == null) delete obj[key]
})
return obj
}
You can remove null
elements with the code above. You can also give your type and it will remove all nested objects properties
本文标签: javascriptRemove null attributes from an Object in TypescriptStack Overflow
版权声明:本文标题:javascript - Remove null attributes from an Object in Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744064995a2584825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论