Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

419
Visualizações
Using rxjs operator is it possible to get second last value

This is the sample code using rxjs operator, I want to print/get the second last value.

typescript

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

//emit (1,2,3,4,5)
const source = from([1, 2, 3, 4, 5]);
//add 10 to each value
const example = source.pipe(map(val => val + 10)).pipe(last());
//output: 11,12,13,14,15
const subscribe = example.subscribe(val => console.log(val));

Currently it prints 15 but I want it to print 14 instead.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can make use of the takeLast() and skipLast RxJS operator.

The takeLast() operator allows you to

Emits only the last count values emitted by the source Observable.

while the skipLast() operator

Skip the last count values emitted by the source Observable.

Now, we can combine both pipeable operators such that we will take the last 2 counts, and skip the last count.

import { range, from } from 'rxjs';
import { takeLast, map, skipLast} from 'rxjs/operators';

const source = from([1, 2, 3, 4, 5]);

const example = source
  .pipe(
    map(val => val + 10),
    takeLast(2), 
    skipLast(1)
  );

example.subscribe(res => console.log(res));

Here is a demo.

over 4 years ago · Santiago Trujillo Relatório

0

You can use the pairwise operator: https://www.learnrxjs.io/operators/combination/pairwise.html

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

//emit (1,2,3,4,5)
const source = from([1, 2, 3, 4, 5]);
//add 10 to each value
const example = source.pipe(map(val => val + 10)).pipe(pairwise(),last());
//output: 11,12,13,14,15
const subscribe = example.subscribe(val => console.log(val[0]));
over 4 years ago · Santiago Trujillo Relatório

0

  • takeLast take last n emitted values before completion

  • take will take the first n number

import { from } from "rxjs";
import { take, takeLast } from "rxjs/operators";

const source = from([1,2,3,4,5]);

source
  .pipe(
    takeLast(2), 
    take(1) 
  )
  .subscribe(console.log);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda