mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Conditionally Include Pairs In An Object as a javascript til
This commit is contained in:
35
javascript/conditionally-include-pairs-in-an-object.md
Normal file
35
javascript/conditionally-include-pairs-in-an-object.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Conditionally Include Pairs In An Object
|
||||
|
||||
You can add key-value pairs to an object using the ES6 spread operator:
|
||||
|
||||
```javascript
|
||||
> { one: 1, ...{ hello: "world" } }
|
||||
{ one: 1, hello: "world" }
|
||||
```
|
||||
|
||||
By combining the spread operator with some boolean logic, you can conditionally
|
||||
add key-value pairs to an object:
|
||||
|
||||
```javascript
|
||||
> {
|
||||
one: 1,
|
||||
...(isArriving && { hello: "world" }),
|
||||
}
|
||||
```
|
||||
|
||||
Depending on the value of `isArriving`:
|
||||
|
||||
```javascript
|
||||
// isArriving === true
|
||||
{ one: 1, hello: "world" }
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```javascript
|
||||
// isArriving === false
|
||||
{ one: 1 }
|
||||
```
|
||||
|
||||
This is useful for dynamically building up some configuration object or data
|
||||
payload.
|
||||
Reference in New Issue
Block a user