mirror of
https://github.com/antonmedv/fx
synced 2024-11-17 09:25:32 +00:00
Update DOCS.md
This commit is contained in:
parent
e5aef9ff32
commit
e7619aeece
45
DOCS.md
45
DOCS.md
@ -5,6 +5,7 @@
|
||||
+ [Anonymous function](#anonymous-function)
|
||||
+ [Binding](#binding)
|
||||
+ [Dot](#dot)
|
||||
+ [Map](#map)
|
||||
+ [Chaining](#chaining)
|
||||
+ [Updating](#updating)
|
||||
+ [Edit-in-place](#edit-in-place)
|
||||
@ -71,6 +72,50 @@ $ echo '{"foo": "bar"}' | fx .
|
||||
}
|
||||
```
|
||||
|
||||
### Map
|
||||
|
||||
One of the frequent operations is mapping some function on an array. For example, to extract some values.
|
||||
|
||||
```
|
||||
[
|
||||
{
|
||||
"author": {
|
||||
"name": "antonmedv"
|
||||
},
|
||||
...
|
||||
},
|
||||
{...},
|
||||
{...},
|
||||
...
|
||||
]
|
||||
```
|
||||
|
||||
And we want to collect names of each object in array. We can do this by mapping anonymous function:
|
||||
|
||||
```bash
|
||||
$ cat ... | fx '.map(x => x.author.name)'
|
||||
```
|
||||
|
||||
Or we can do the same by using `@` prefix:
|
||||
|
||||
```bash
|
||||
$ cat ... | fx @.author.name
|
||||
[
|
||||
"antonmedv",
|
||||
...
|
||||
]
|
||||
```
|
||||
|
||||
Expression followed by `@` symbol will be mapped to each element of array.
|
||||
|
||||
> Note what `@` can be applied to map object values.
|
||||
> ```bash
|
||||
> $ echo '{"foo": 1, "bar": 2}' | fx @+1
|
||||
> [2, 3]
|
||||
> ```
|
||||
>
|
||||
> Also note what symbol `@` alone is equivalent of `Object.values` function.
|
||||
|
||||
### Chaining
|
||||
|
||||
You can pass any number of anonymous functions for reducing JSON:
|
||||
|
Loading…
Reference in New Issue
Block a user