What does the `map` method mean in RxJS?

后端 未结 2 1059
别跟我提以往
别跟我提以往 2020-12-24 08:37

I\'m learning RxJS by reading this tutorial http://reactive-extensions.github.io/learnrx/.

I have a hard time understanding the map method of Obse

2条回答
  •  囚心锁ツ
    2020-12-24 09:05

    Map in Rxjs used for projection, means you can transform the array in to entirely new array. In order to understand how Map works , we can implement our own map function using plain javascript.

    Array.prototype.map = function(projectionFunction){
    var results=[];
    this.forEach(function(item) {
    results.push(projectionFunction(item));
    });
    return results;
    };
    

    You can see I have written a map function which accepts an anonymous function as a parameter. This will be your function to apply the projection to transform the array. Inside the map function you can see iterating each item in a array , call the project function by passing each item and finally the result of the projection function will push to the results array.

    JSON.stringify([1,2,3].map(function(x){return x+1;}))
    

    Output

    [2,3,4]
    

提交回复
热议问题