Files
30-seconds-of-code/snippets/defaults.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

23 lines
713 B
Markdown

---
title: Assign default values for object properties
tags: object
cover: boats
firstSeen: 2018-01-19T13:51:05+02:00
lastUpdated: 2020-10-22T20:23:47+03:00
---
Assigns default values for all properties in an object that are `undefined`.
- Use `Object.assign()` to create a new empty object and copy the original one to maintain key order.
- Use `Array.prototype.reverse()` and the spread operator (`...`) to combine the default values from left to right.
- Finally, use `obj` again to overwrite properties that originally had a value.
```js
const defaults = (obj, ...defs) =>
Object.assign({}, obj, ...defs.reverse(), obj);
```
```js
defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }
```