Object Methods
These are utility methods for handling objects.
pick(object, paths)
Creates an object composed of the picked object properties.
Usage
import { pick } from 'xfunc'
const object = { a: 1, b: '2', c: 3 }
pick(object, ['a', 'c'])
// => { a: 1, c: 3 }Arguments
object(Object): The source objectpaths(Array): The property paths to pick
Returns
(Object): Returns the new object
pickBy(object, predicate)
Creates an object composed of the object properties predicate returns truthy for.
Usage
import { pickBy } from 'xfunc'
const object = { a: 1, b: '2', c: 3 }
pickBy(object, (value) => typeof value === 'number')
// => { a: 1, c: 3 }Arguments
object(Object): The source objectpredicate(Function): The function invoked per property
Returns
(Object): Returns the new object
omit(object, paths)
Creates an object composed of the own and inherited enumerable property paths of object that are not omitted.
Usage
import { omit } from 'xfunc'
const object = { a: 1, b: '2', c: 3 }
omit(object, ['a', 'c'])
// => { b: '2' }Arguments
object(Object): The source objectpaths(Array): The property paths to omit
Returns
(Object): Returns the new object
omitBy(object, predicate)
Creates an object composed of the own and inherited enumerable string keyed properties of object that predicate doesn't return truthy for.
Usage
import { omitBy } from 'xfunc'
const object = { a: 1, b: '2', c: 3 }
omitBy(object, (value) => typeof value === 'number')
// => { b: '2' }Arguments
object(Object): The source objectpredicate(Function): The function invoked per property
Returns
(Object): Returns the new object
mapEntries(object, iteratee)
Creates an object with the same keys as object and values generated by running each own enumerable string keyed property of object thru iteratee.
Usage
import { mapEntries } from 'xfunc'
const object = { a: 1, b: 2 }
mapEntries(object, ([key, value]) => [key.toUpperCase(), value * 2])
// => { A: 2, B: 4 }Arguments
object(Object): The object to iterate overiteratee(Function): The function invoked per iteration
Returns
(Object): Returns the new mapped object
forEachEntry(object, iteratee)
Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property.
Usage
import { forEachEntry } from 'xfunc'
forEachEntry({ a: 1, b: 2 }, ([key, value]) => {
console.log(key, value)
})
// => Logs 'a 1' then 'b 2'Arguments
object(Object): The object to iterate overiteratee(Function): The function invoked per iteration
Returns
(Object): Returns object
forOwn(object, iteratee)
Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property. The iteratee is invoked with three arguments: (value, key, object). Iteratee functions may exit iteration early by explicitly returning false.
Usage
import { forOwn } from 'xfunc'
forOwn({ a: 1, b: 2 }, (value, key) => {
console.log(key, value)
})
// => Logs 'a 1' then 'b 2'
// Exit early by returning false
forOwn({ a: 1, b: 2, c: 3 }, (value, key) => {
console.log(key, value)
if (key === 'b') return false
})
// => Logs 'a 1' then 'b 2'Arguments
object(Object): The object to iterate overiteratee(Function): The function invoked per iteration. Receives(value, key, object)as arguments
Returns
(Object): Returns object
hasOwn(object, key)
Checks if an object has a specific own property.
Usage
import { hasOwn } from 'xfunc'
hasOwn({ a: 1 }, 'a')
// => true
hasOwn({ a: 1 }, 'b')
// => false
hasOwn({ a: 1 }, 'toString')
// => false (inherited property)Arguments
object(Object): The object to checkkey(string|symbol): The property key to check for
Returns
(boolean): Returns true if object has own property, false otherwise
clone(source)
Creates a shallow clone of value.
Usage
import { clone } from 'xfunc'
const original = { a: 1, b: { c: 2 } }
const cloned = clone(original)
console.log(cloned)
// => { a: 1, b: { c: 2 } }
console.log(cloned === original)
// => false
console.log(cloned.b === original.b)
// => true (nested objects are shared)Arguments
source(any): The value to clone
Returns
(any): Returns the cloned value
Notes
- Primitive values are returned as-is
- Arrays and objects are shallow cloned
- Nested objects/arrays are not cloned (references are shared)
cloneDeep(source)
Creates a deep clone of value.
Usage
import { cloneDeep } from 'xfunc'
const original = { a: 1, b: { c: 2 } }
const cloned = cloneDeep(original)
console.log(cloned)
// => { a: 1, b: { c: 2 } }
console.log(cloned === original)
// => false
console.log(cloned.b === original.b)
// => false (nested objects are also cloned)Arguments
source(any): The value to clone
Returns
(any): Returns the deep cloned value
Notes
- Primitive values are returned as-is
- All nested objects and arrays are recursively cloned
- Handles circular references safely
merge(target, ...sources)
Deep merge multiple objects into a target object, with type-safe recursive merging of nested objects and array concatenation.
Usage
import { merge } from 'xfunc'
const target = { a: 1, b: { c: 2, d: [1, 2] } }
const source = { b: { c: 10, e: 3 }, f: 4 }
merge(target, source)
// => { a: 1, b: { c: 10, d: [1, 2], e: 3 }, f: 4 }
// Array concatenation
merge({ arr: [1, 2] }, { arr: [3, 4] })
// => { arr: [1, 2, 3, 4] }
// Multiple sources
merge({ a: 1 }, { b: 2 }, { c: 3 })
// => { a: 1, b: 2, c: 3 }Arguments
target(Object): The destination object...sources(Object[]): The source objects
Returns
(Object): Returns target
Notes
- Mutates and returns the target object
- Nested objects are recursively merged
- Arrays are concatenated by default
- Non-plain objects (Date, etc.) are not merged