# Defaults Object

Apply default values to an object using lodash _.defaults

> Canonical page: https://elysiatools.com/en/tools/defaults-object

- **Category:** Data Processing

- **Keywords:** defaults, object, lodash, fill, properties

## Overview

Assign default values for undefined properties on the target object.

**Highlights:**
- Uses lodash `_.defaults`
- Only fills in undefined properties
- Later sources are applied first

**Example:**
- Target: `{"a":1}`
- Source 1: `{"a":2,"b":2}`
- Result: `{"a":1,"b":2}`

## Inputs

- **Target Object** (textarea): Enter the target JSON object
- **Source 1** (textarea): First source object
- **Source 2** (textarea): Optional source object
- **Source 3** (textarea): Optional source object

## When to use

- When you need to ensure an object has required keys without overwriting existing data.
- When merging configuration objects where user-defined settings should take precedence over defaults.
- When cleaning up incomplete data structures by filling in missing fields with predefined values.

## How it works

- Input your primary target JSON object into the target field.
- Provide one or more source objects containing the default values you wish to apply.
- The tool processes the objects, assigning values from sources only to properties that are currently undefined in the target.
- The resulting JSON object is returned with all missing properties filled.

## Use cases

- Applying default configuration settings to a user's partial preferences object.
- Filling in missing API response fields with fallback values to prevent runtime errors.
- Standardizing data objects by ensuring all expected keys exist with default values.

## Frequently asked questions

### Does this tool overwrite existing values?

No, it only assigns values to properties that are undefined in the target object.

### What happens if multiple sources have the same key?

The tool applies sources in order; earlier sources take precedence over later ones for the same undefined property.

### Which library does this tool use?

It utilizes the lodash _.defaults function for reliable object merging.

### Can I use this for nested objects?

This tool performs a shallow merge; it assigns values to top-level undefined properties.

### Is the input format strictly JSON?

Yes, the tool expects valid JSON objects in the input fields.

## Related tools

- [Assign Object](https://elysiatools.com/en/tools/assign-object): Assign source properties to a target object using lodash _.assign
- [Fill Array](https://elysiatools.com/en/tools/fill-array): Fill array with value from start to end index using lodash _.fill
- [From Pairs](https://elysiatools.com/en/tools/from-pairs): Convert array of key-value pairs to object using lodash _.fromPairs
- [Pull Values](https://elysiatools.com/en/tools/pull-values): Remove specific values from an array using lodash _.pull
- [Without Values](https://elysiatools.com/en/tools/without-values): Remove specified values from an array using lodash _.without
- [Zip Object](https://elysiatools.com/en/tools/zip-object): Create an object from keys and values using lodash _.zipObject
- [Compact Array](https://elysiatools.com/en/tools/compact-array): Remove falsey values from array using lodash _.compact
- [Flatten Depth Array](https://elysiatools.com/en/tools/flatten-depth-array): Flatten array to specified depth using lodash _.flattenDepth

## Samples

- [Grafana Advanced Application Samples](https://elysiatools.com/en/samples/grafana-samples): Comprehensive Grafana samples covering advanced dashboard design, alerting configurations, data source integrations, and plugin development
- [QR Code Samples](https://elysiatools.com/en/samples/qrcode-samples): Sample QR code images for testing QR code reading and generation tools
- [D3.js Data Visualization Samples](https://elysiatools.com/en/samples/d3js-data-visualization): Comprehensive D3.js data visualization examples including charts, maps, animations, and interactive visualizations
- [Rome JavaScript Toolchain Samples](https://elysiatools.com/en/samples/rome): Rome JavaScript toolchain examples including configuration, linting, formatting, bundling, and project management
