简体   繁体   English

Array map和rxjs map之间有什么区别

[英]What are the differences between Array map and rxjs map

I was wondering what the map on both rxjs and array works the same way. 我想知道rxjs和数组上的映射是一样的。 What are the differences between the uses of both the array map method and rxjs map operator? 数组映射方法和rxjs映射运算符的使用有何不同?

Array.map transforms each element of a single array. Array.map转换单个数组的每个元素

console.log( [ 1, 2, 3 ].map(x => x * x) )
// log: [ 1, 4, 9 ]

In general, RXJS Observables are more like a stream of data, but each data is its own entity. 通常,RXJS Observables更像是一个数据流,但每个数据都是它自己的实体。

You may choose to store arrays in your Observable, but still, each array is treated like a single entity. 您可以选择在Observable中存储数组,但仍然会将每个数组视为单个实体。 Every time you call Subject#next , you're providing an entirely new array. 每当你调用Subject#next ,你就会提供一个全新的数组。 In this scenario, there is no equivalent to Array#push with RXJS, because RXJS doesn't care that the content of the Observable happens to be an array. 在这种情况下,没有与使用RXJS的Array#push等效,因为RXJS并不关心 Observable的内容恰好是一个数组。

// Observable that holds an array as its type
const subject: Subject<number[]> = new Subject<number[]>();
subject.pipe(
  // the value here is a full array
  map(arr => arr.map(x => x * x))
).subscribe(arr => console.log(arr));

subject.next([ 1, 2, 3 ]);
// log: [ 1, 4, 9 ]

subject.next([ 7, 8, 9 ]);
// log: [ 49, 64, 81 ]

* Bonus : You can kinda make something act more like an array if you set up a ReplaySubject . *优点:您可以还挺使一些行为更像是一个数组,如果你设置了ReplaySubject This implementation of Subject literally replays all the data that was given to it (or a subset based on how you instantiate it). Subject这个实现实际上重放了给它的所有数据(或者基于你如何实例化它的子集)。 As you'll see though, the limitation to this would be that you can only push onto the end, and you have to create a new subscription to see the entire "array", but it's an interesting thought experiment nonetheless. 正如您所看到的,对此的限制将是您只能推到最后,并且您必须创建一个新的订阅以查看整个“数组”,但它仍然是一个有趣的思想实验。

const subject: ReplaySubject<number> = new ReplaySubject<number>();
subject.next(1);
subject.next(2);
subject.next(3);

const transformed: Observable<number> = subject.pipe(
  map(x => x * x)
);

transformed.pipe(first()).subscribe(x => console.log(x));
// log: 1
// log: 4
// log: 9

subject.next(9);
transformed.pipe(first()).subscribe(x => console.log(x));
// log: 1
// log: 4
// log: 9
// log: 81

Their functioning is quite the same, they give a new Array / Observable (respectively) with each element transformed according to (usually) a transformation function (the technical computer science name would be a projection ), taking as parameter the modified object and its index. 它们的功能完全相同,它们分别给出一个新的Array / Observable,每个元素根据(通常)转换函数(技术计算机科学名称将是一个投影 )进行转换,将修改后的对象及其索引作为参数。

Array.map is part of the prototype of Array natively. Array.map就是Array原型的一部分。 You can use it on any array in any JavaScript environment. 您可以在任何JavaScript环境中的任何阵列上使用它。 (provided you didn't mess up the Array.prototype, of course) (当然你没有弄乱Array.prototype)

Observable.map must be imported. 必须导入Observable.map (For RxJS 6 : import { map } from 'rxjs/operators'; , for older versions : import { map } from 'rxjs/add/operator/map' (对于RxJS 6: import { map } from 'rxjs/operators';对于旧版本: import { map } from 'rxjs/add/operator/map'


There is a small but significant difference 有一个小但重要的区别

Array.map transformation function has access to the whole array being transformed (the third parameter in the projection function). Array.map转换函数可以访问正在转换整个数组 (投影函数中的第三个参数)。

Exemple : 例如:

  let arr = ['a', 'b', 'c']; let arrResult = arr.map( (elem, index, wholeArray) => 'element ' + elem + ' was in position ' + index + ' in array ' + wholeArray); console.log(arrResult); 

This is "of course" not possible (generally speaking) in the context of Observable since the "whole list" of emitted value is probably not known at the time each element is emitted. 这在“Observable”的上下文中“当然”是不可能的(一般来说),因为在发出每个元素时可能不知道发射值的“整个列表”。


On the opposite, there is another small difference : Observable.map operator take an (optional) thisArg parameter, so the transformation function has access to some specified context. 相反,还有另一个小的区别: Observable.map运算符采用(可选) thisArg参数,因此转换函数可以访问某些指定的上下文。

I think this other difference is quite insignificant : Array.map doesn't need this, because it is a function and you can also specify your own " this " with the various ways of calling functions in JavaScript. 我认为这另一个区别是非常微不足道的: Array.map不需要这个,因为它是一个函数,您还可以使用各种方法在JavaScript中调用函数来指定自己的“ this ”。 (I don't find a nice link for this part, anyone who want to add a reference for this in this answer is welcome). (我没有找到这个部分的一个很好的链接,任何想要在这个答案中添加参考的人都是受欢迎的)。

Also, I would find interesting to be challenged on that last point anyway. 此外,无论如何,我觉得有趣的是在最后一点上受到挑战。

RxJS is for working with Observables and native map is for Arrays . RxJS用于处理Observables而原生map用于Arrays Thats the only diffrence I can think of. 这是我能想到的唯一差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM