Question: In HTML, there is a `<a>` that redirects to another component, and after that scrolls to a certain anchor.

In the target component, I have the following code

```
  ngAfterViewInit(): void {
    this.route.fragment.subscribe(fragment => {
      const elmntLoc: HTMLElement = document.getElementById(fragment);
      window.scroll({
        top: elmntLoc.offsetTop,
        behavior: "smooth"
      });
    });
  }

```

However, I found that `document.getElementById(fragment);` is always `null`, because this element is displayed conditionally using `ngIf = booleanVariable`, and when this `ngAfterViewInit` life cycle hook runs, this `booleanVariable` is not calculated yet.

I am wondering how to make sure `ngAfterViewInit` runs after `booleanVariable` is calculated and therefore this element is rendered?

I tried to use `setTime` but it seems hacky...

Thanks in advance!

Answer: 
You have a stream of objects. You will need to group all those document into one to work on them collectively.

There are two ways to achieve this:

* Using "slurp mode" (`--slurp`/`-s`)
* Using `[ inputs ]`, which requires `-null-input`/`-n`.

---

The number of documents in the stream:

```sh
jq -sr length

```

```sh
jq -nr '[ inputs ] | length'

```

---

The number of documents whose root object have value `foo` for key `name`:

```sh
jq -sr 'map( select( .name == "foo" ) ) | length'

```

```sh
jq -nr '[ inputs | select( .name == "foo" ) ] | length'

```

---

The titles grouped by name:

```sh
jq -s 'reduce .[] as $doc ( {}; .[ $doc.name ] += [ $doc.title ] )'

```

```sh
jq -n 'reduce inputs as $doc ( {}; .[ $doc.name ] += [ $doc.title ] )'

```
To avoid slurping and any other method of creating an array in order to take its length, you can use the following stream-oriented counting function:

```
def count(s): reduce s as $x (0; .+1);

```

For example, instead of:

```
jq -nr '[ inputs ] | length'

```

you could write:

```
jq -n -f count.jq

```

where count.jq contains:

```
def count(s): reduce s as $x (0; .+1);
count(inputs)

```

Similarly, instead of

```
 jq -nr '[ inputs | select( .name == "foo" ) ] | length'

```

you would use the same "def" with:

```
count(inputs | select( .name == "foo" ))

```