39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
/**
|
|
* Replaces undefined values with null in an object.
|
|
* Optionally, you can specify keys to replace.
|
|
* If keys are specified, only those keys will be replaced.
|
|
* If no keys are specified, all undefined values will be replaced.
|
|
* @param obj
|
|
* @param keys
|
|
* @returns {*}
|
|
* @constructor
|
|
*/
|
|
export default function UndefinedToNull(obj, keys) {
|
|
Object.keys(obj).forEach((key) => {
|
|
if (keys && keys.indexOf(key) >= 0) {
|
|
if (obj[key] === undefined) obj[key] = null;
|
|
} else {
|
|
if (obj[key] === undefined) obj[key] = null;
|
|
}
|
|
});
|
|
return obj;
|
|
}
|
|
|
|
/**
|
|
* Replaces undefined values with null in an object. Optionally, you can specify keys to replace. If keys are specified, only those keys will be replaced. If no keys are specified, all undefined values will be replaced.
|
|
* @param obj
|
|
* @param keys
|
|
* @returns {{[p: string]: unknown}}
|
|
*/
|
|
export function replaceUndefinedWithNull(obj, keys) {
|
|
return Object.fromEntries(
|
|
Object.entries(obj).map(([key, value]) => {
|
|
if (keys) {
|
|
return [key, keys.includes(key) && value === undefined ? null : value];
|
|
} else {
|
|
return [key, value === undefined ? null : value];
|
|
}
|
|
})
|
|
);
|
|
}
|