omitUndefined

Return a new object with undefined-valued properties removed, a clean way to drop optional fields before serializing or merging.

Usage

The omitUndefined function removes properties whose value is undefined from an object.

import { omitUndefined } from '@movk/core'

const data = {
  name: 'John',
  age: undefined,
  city: 'New York',
  country: undefined
}

const cleaned = omitUndefined(data)
console.log(cleaned) // { name: 'John', city: 'New York' }

// Clean data before an API request
const requestData = omitUndefined({
  title: 'Post Title',
  content: 'Post content',
  tags: undefined,
  published: true
})

API

omitUndefined(obj)

Removes properties whose value is undefined from an object.

Parameters

obj
T required
The source object.

Returns

returns
Partial<T>
A new object with undefined-valued properties removed.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License