Last active
September 24, 2019 17:13
-
-
Save raloliver/6be0e9d36a9bd14207828b24393a76d7 to your computer and use it in GitHub Desktop.
RxJS: map, filter, take, find, first, last and pluck. (https://codepen.io/raloliver/pen/vYBbwbM)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//altera a estrutura da entrada e transforma numa outra estrutura | |
var itemsMap = [{a: 1}, {a: 2}, {a: 3}, {a: 4},]; | |
var sourceMap = Rx.Observable.from(items); | |
var subscriptionMap = source.map(x => { return {value: x.a} } ).subscribe(x => console.log(x)); | |
//apenas os números pares retornam | |
var sourceFilter = Rx.Observable.from([1,2,3,4,5,6,7,8,9,10]); | |
var subscriptionFilter = source.filter(x => x % 2 === 0 ).subscribe(x => console.log(x)); | |
//apenas um determinado número de elementos | |
var sourceTake = Rx.Observable.from([1,2,3,4,5,6,7,8,9,10]); | |
var subscriptionTake = source.take(3).subscribe(x => console.log(x)) | |
//alguém com idade maior que 18 | |
var peopleFind = [{name: 'Rick', age: 10},{name: 'Rose', age: 25},{name: 'Cassandra', age: 13},{name: 'Henry', age: 22}] | |
var sourceFind = Rx.Observable.from(people); | |
var subscriptionFind = source.find(x => x.age > 18).subscribe(x => console.log(x)) | |
//primeiro elemento que passe na condição (nome que inicie com a letra R) | |
var peopleFirst = [{name: 'Rick', age: 10},{name: 'Rose', age: 25},{name: 'Cassandra', age: 13},{name: 'Henry', age: 22}] | |
var sourceFirst = Rx.Observable.from(people); | |
var subscriptionFirst = source.first(x => x.name[0] === 'R').subscribe(x => console.log(x)) | |
//último elemento que passe na condição (nome que inicie com a letra R) | |
var peopleLast = [{name: 'Rick', age: 10},{name: 'Rose', age: 25},{name: 'Cassandra', age: 13},{name: 'Henry', age: 22}] | |
var sourceLast = Rx.Observable.from(people); | |
var subscriptionLast = source.last(x => x.name[0] === 'R').subscribe(x => console.log(x)) | |
//retorna apenas os valores da propriedade selecionada | |
var peoplePluck = [{name: 'Rick', age: 10},{name: 'Rose', age: 25},{name: 'Cassandra', age: 13},{name: 'Henry', age: 22}] | |
var source = Rx.Observable.from(people); | |
var subscription = source.pluck('name').subscribe(x => console.log(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment