


Why is there no output when using RxJS to process stream elements? How to use of and from operators correctly?
RxJS Streaming: of
the correct use of from
operator
When using RxJS to process data streams, it is crucial to properly select operators. This article discusses a common RxJS stream processing problem: when using of
and from
operators to process array elements, the output results do not meet expectations.
Problem description:
Goal: Filter out even numbers from an array containing numbers and multiply the even numbers by 2.
Error code:
import { of } from 'rxjs'; import { map, filter } from 'rxjs/operators'; const e = of([1, 2, 3, 4, 5]); e.pipe( filter(item => item % 2 === 0), map(num => num * 2) ).subscribe(v => console.log(v));
After running this code, there is no output. This is because the of
operator emits the entire array [1, 2, 3, 4, 5]
as a single element instead of each element in the array separately. Therefore, item
received by filter
and map
operators is the entire array, resulting in the conditional judgment and multiplication results not meeting expectations.
Solution:
In order to emit each element in the array separately, the from
operator is required. The from
operator can convert an iterable object such as an array, string, and other objects into an Observable, which emits each element in the object in sequence.
Correct code:
import { from } from 'rxjs'; import { map, filter } from 'rxjs/operators'; const e = from([1, 2, 3, 4, 5]); e.pipe( filter(item => item % 2 === 0), map(num => num * 2) ).subscribe(v => console.log(v));
In this code, from([1, 2, 3, 4, 5])
converts the array into an Observable, which emits 1, 2, 3, 4, 5 in turn. filter
operator filters out even numbers 2 and 4, and map
operator multiplies them by 2, and the final output is 4 and 8.
Through this example, we can clearly see the difference between of
and from
operators when processing arrays, and how to select the correct operator to achieve the expected stream processing results. Remember, of
emits a single value, and from
emits each element in the iterable object.
The above is the detailed content of Why is there no output when using RxJS to process stream elements? How to use of and from operators correctly?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

The state of the CentOS firewall can be viewed through the sudo firewall-cmd --state command, returning to running or not running. For more detailed information, you can use sudo firewall-cmd --list-all to view, including configured areas, services, ports, etc. If firewall-cmd does not solve the problem, you can use sudo iptables -L -n to view iptables rules. Be sure to make a backup before modifying the firewall configuration to ensure server security.

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.
