mirror of
https://github.com/jbranchaud/til
synced 2026-01-21 16:08:03 +00:00
Compare commits
2 Commits
c1f5486660
...
2978c9493d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2978c9493d | ||
|
|
1ac43cce9c |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1229 TILs and counting..._
|
_1231 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -342,6 +342,7 @@ _1229 TILs and counting..._
|
|||||||
|
|
||||||
### GROQ
|
### GROQ
|
||||||
|
|
||||||
|
- [Grab Multiple Values From A Reference](groq/grab-multiple-values-from-a-reference.md)
|
||||||
- [Grab Values From An Array Of References](groq/grab-values-from-an-array-of-references.md)
|
- [Grab Values From An Array Of References](groq/grab-values-from-an-array-of-references.md)
|
||||||
- [Include Type Of Operation In Webhook Response](groq/include-type-of-operation-in-webhook-response.md)
|
- [Include Type Of Operation In Webhook Response](groq/include-type-of-operation-in-webhook-response.md)
|
||||||
|
|
||||||
@@ -431,6 +432,7 @@ _1229 TILs and counting..._
|
|||||||
- [Object Initialization With Shorthand Property Names](javascript/object-initialization-with-shorthand-property-names.md)
|
- [Object Initialization With Shorthand Property Names](javascript/object-initialization-with-shorthand-property-names.md)
|
||||||
- [Obtain Undefined Value With The Void Operator](javascript/obtain-undefined-value-with-the-void-operator.md)
|
- [Obtain Undefined Value With The Void Operator](javascript/obtain-undefined-value-with-the-void-operator.md)
|
||||||
- [Parse A Date From A Timestamp](javascript/parse-a-date-from-a-timestamp.md)
|
- [Parse A Date From A Timestamp](javascript/parse-a-date-from-a-timestamp.md)
|
||||||
|
- [Purge Null And Undefined Values From Object](javascript/purge-null-and-undefined-values-from-object.md)
|
||||||
- [Random Cannot Be Seeded](javascript/random-cannot-be-seeded.md)
|
- [Random Cannot Be Seeded](javascript/random-cannot-be-seeded.md)
|
||||||
- [Reach Into An Object For Nested Data With Get](javascript/reach-into-an-object-for-nested-data-with-get.md)
|
- [Reach Into An Object For Nested Data With Get](javascript/reach-into-an-object-for-nested-data-with-get.md)
|
||||||
- [Render An Array Of Elements With React 16](javascript/render-an-array-of-elements-with-react-16.md)
|
- [Render An Array Of Elements With React 16](javascript/render-an-array-of-elements-with-react-16.md)
|
||||||
|
|||||||
43
groq/grab-multiple-values-from-a-reference.md
Normal file
43
groq/grab-multiple-values-from-a-reference.md
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# Grab Multiple Values From A Reference
|
||||||
|
|
||||||
|
Let's say we have an `author` with some attributes including a reference to a
|
||||||
|
`person` which contains more data about the person. Here is one way to write a
|
||||||
|
query to access that data.
|
||||||
|
|
||||||
|
```groq
|
||||||
|
*[_type == 'author' && slug.current == 'donna-tartt']{
|
||||||
|
website,
|
||||||
|
'firstName': person->firstName,
|
||||||
|
'lastName': person->lastName,
|
||||||
|
'age': person->age
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here is another way to write this query that doesn't do three separate accesses
|
||||||
|
on the `person` reference.
|
||||||
|
|
||||||
|
```groq
|
||||||
|
*[_type == 'author' && slug.current == 'donna-tartt']{
|
||||||
|
website,
|
||||||
|
person-> {
|
||||||
|
'firstName': firstName,
|
||||||
|
'lastName': lastName,
|
||||||
|
'age': age
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This isn't quite right though because it leaves the three reference values
|
||||||
|
nested under `person`. We can get back to the original shape of our query by
|
||||||
|
flattening the `person` object using familiar looking spread syntax (`...`).
|
||||||
|
|
||||||
|
```groq
|
||||||
|
*[_type == 'author' && slug.current == 'donna-tartt']{
|
||||||
|
website,
|
||||||
|
...person-> {
|
||||||
|
'firstName': firstName,
|
||||||
|
'lastName': lastName,
|
||||||
|
'age': age
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
40
javascript/purge-null-and-undefined-values-from-object.md
Normal file
40
javascript/purge-null-and-undefined-values-from-object.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# Purge Null And Undefined Values From Object
|
||||||
|
|
||||||
|
The low-level utilities provided by [`lodash`](https://lodash.com/) offer a
|
||||||
|
couple ways to remove all the `null` and `undefined` values from an object.
|
||||||
|
|
||||||
|
First, here is an object that I want to _compact_ by removing all `nil` (`null`
|
||||||
|
and `undefined`) values.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const data = {
|
||||||
|
hello: 'world',
|
||||||
|
list: [1,2,3],
|
||||||
|
status: undefined,
|
||||||
|
published_at: null,
|
||||||
|
points: 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
One method of doing this is with the [`_.pickBy`
|
||||||
|
function](https://lodash.com/docs/4.17.15#pickBy).
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
> _.pickBy(data)
|
||||||
|
//=> { hello: 'world', list: [1,2,3] }
|
||||||
|
```
|
||||||
|
|
||||||
|
Because it defaults to picking _truthy_ values, the `points: 0` is also going
|
||||||
|
to be stripped out.
|
||||||
|
|
||||||
|
Another method which allows us to more directly target just `null` and
|
||||||
|
`undefined` uses [`_.omitBy`](https://lodash.com/docs/4.17.15#omitBy) and
|
||||||
|
[`_.isNil`](https://lodash.com/docs/4.17.15#isNil).
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
> _.omitBy(data, _.isNil)
|
||||||
|
//=> { hello: 'world', list: [1,2,3], points: 0 }
|
||||||
|
```
|
||||||
|
|
||||||
|
Notice this approach only removes the `null` and `undefined` key-value pairs.
|
||||||
|
The `points: 0` is left in.
|
||||||
Reference in New Issue
Block a user