Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular map. What is it?

I am learning Angular 6 and I am puzzled with constructions like a:

this.contentArray.map((v: string, i: number) => `Content line ${i + 1}`)

or like a:

return this.aService.getItems()
    .pipe(map(response => response.data));

I am run through the couples of book like a "The_Complete_Book_on_Angular_6" or "Pro Angular 6" (Adam Freeman) but there are no simple explanations there. Google kept silence about that too. Can someone give the right and good tutorial or may be book (for amateurs) about array.map, array.filter and about .pipe(map(...))?

like image 645
Anton Romanov Avatar asked Dec 13 '18 10:12

Anton Romanov


1 Answers

The first one is simply Array.prototype.map, while the second one is the rxjs map operator.

In the first case, you take an existing array and apply a function to each of its elements

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

[1, 2, 3, 4].map(x => x + 2) // [3, 4, 5, 6]

while the second case is essentialy the same thing in the world of observables.

Applies a given project function to each value emitted by the source Observable, and emits the resulting values as an Observable.

like image 187
bugs Avatar answered Oct 09 '22 16:10

bugs