# ghostkit/3.3.2/gutenberg/utils/compact-object/index.js

Block Animations, Motion &amp; Scroll Effects – Ghost Kit, version 3.3.2. 27 lines.

- Page: https://pluginprobe.com/plugins/ghostkit/3.3.2/code/gutenberg/utils/compact-object/index.js
- Raw: https://pluginprobe.com/plugins/ghostkit/3.3.2/raw/gutenberg/utils/compact-object/index.js
- Modified: 2024-02-18T18:40:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ghostkit/3.3.2/code/gutenberg/utils/compact-object/index.js#L10-L20`.

```javascript
/**
 * Compact object - remove empty nested objects and undefined values.
 *
 * @link https://gist.github.com/Mazuh/8209a608a655f91b9de319872d7a660a
 *
 * @param {Object} obj - object to work with.
 *
 * @return {Object}
 */
export default function compactObject(obj) {
	if (typeof obj !== 'object') {
		return obj;
	}

	return Object.keys(obj).reduce(function (accumulator, key) {
		const isObject = typeof obj[key] === 'object';
		const value = isObject ? compactObject(obj[key]) : obj[key];
		const isEmptyObject = isObject && !Object.keys(value).length;

		if (value === undefined || isEmptyObject) {
			return accumulator;
		}

		return Object.assign(accumulator, { [key]: value });
	}, {});
}

```
