Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is exact difference between pipe and map in angular 7?

I have searched a lot about this but i am unable to find difference between pipe and map in angular 7? Is it necessary to use pipe in Service.ts file in angular 7?

Thanks

like image 208
manu Avatar asked Jan 10 '19 05:01

manu


People also ask

What is the difference between pipe and map in Angular?

Note: pipe() is a function/method that is used to chain multiple RxJS operators while map() and filter() are operators that operate and transform the values of an Observable (sequence of values).

What is difference between map and TAP in Angular?

The map operator will simply apply a function to that data and return the result. The tap operator however takes a data, apply a function to that data but returns the original data, if the function bothered to return a result, tap just ignores it.

What is the difference between pipe and subscribe in Angular?

The pipe method is for chaining observable operators, and the subscribe is for activating the observable and listening for emitted values.

What is map () in Angular?

The Angular observable Map operator takes an observable source as input. It applies a project function to each of the values emitted by the source observable and transforms it into a new value. It then emits the new value to the subscribers.


3 Answers

With rxjs 5.5 and above, if you want to use any operator on an observable you just pipe them. So here map is just one of those operators within the pipe.

ex:

const example = source.pipe(map(val => val + 10), first());

For earlier versions of rxjs there was no pipe keyword, Multiple operators were combined using . notation

ex:

const example = source.map(val => val + 10).first();
like image 197
Amit Chigadani Avatar answered Oct 26 '22 15:10

Amit Chigadani


map(), filter(), concat(), and flatMap() these are the operators offered by the RxJS library. You can use pipes to link these operators together. Pipes let you combine multiple functions into a single function as follows:

import { filter, map } from 'rxjs/operators';

const squareOddVals = pipe(
filter((n: number) => n % 2 !== 0),
map(n => n * n)
);
like image 26
Sneha Pawar Avatar answered Oct 26 '22 14:10

Sneha Pawar


for short explanation, pipe in RxJS, is used to intercept the result and modify it using RxJS operators so that when you subscribe it you will get the final result equivalent to the logic operators you set in the pipe example map, tap and many more.

for angular practice guide check it here https://angular.io/guide/rx-library

you can see the list of operators here https://www.learnrxjs.io/operators/

like image 28
John Velasquez Avatar answered Oct 26 '22 15:10

John Velasquez