Retrieve the last item from a repeated list in RxJS

As I receive multiple streams from a Socket, my goal is to extract the most recent item.

While distinctUntilChanged function typically selects the first item, in this case, I need to reverse this behavior somehow.

To achieve this using rxjs, I am aiming to transform the stream as follows:

In:  1 - 1 - 1 - 2 - 2 - 3 - 3 - 1 - 1 - 1 - 2 - 2 - 2 - 1 - 2 - 2 - 3 - 3
Out: - - - - 1 - - - 2 - - - 3 - - - - - 1 - - - - - 2 - 1 - - - 2 - 3 - -

Answer №1

We can output the previous value when it changes:

In:  1 - 1 - 1 - 2 - 2 - 3 - 3 - 1 - 1 - 1 - 2 - 2 - 2 - 1 - 2 - 2 - 3 - 3
Out: - - - - - - 1 - - - 2 - - - 3 - - - - - 1 - - - - - 2 - 1 - - - 2 - 3

import { pairwise, take, filter, map, merge, last } from 'rxjs/operators';
import { interval, from } from 'rxjs';

const values$ = from([1, 1, 1, 2, 2, 3, 3, 1, 1, 1, 2, 2, 2, 1, 2, 2, 3, 3]);

values$
  .pipe(
    pairwise(),
    filter(([prev, cur]) => prev !== cur),
    map(([prev, cur]) => prev),
    merge(values$.pipe(last()))
  )
  .subscribe(console.log);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with MUI auto import suggestions in my Next.js 13 project on VS Code

I'm currently developing a project using Next.js 13 and have installed MUI. However, I am encountering an issue where VS Code is not providing auto imports from the @mui/material library, as shown in the attached screenshot. https://i.stack.imgur.com ...

Tips for implementing a wait time for individual items in bee-queue

I've encountered an issue with the delayUntil function in the bee-queue library when creating a queue. await queue .createJob(process) .timeout(60 * 1000 * 2) .retries(2) .backoff('fixed', 60 * 1000) ...

Unwrapping nested objects in a JSON array with JavaScript: A step-by-step guide

After trying some code to flatten a JSON, I found that it flattened the entire object. However, my specific requirement is to only flatten the position property. Here is the JSON array I am working with: [{ amount:"1 teine med 110 mtr iletau" comment:"" ...

What is the best method for obtaining the link URL using JavaScript?

Below is the code I am currently working with: <?php $i = 0; foreach($this->list as $l) { $link = JRoute::_("index.php?option=com_ecommerce&view=detail&id=$l->id"); <div class="quickview" id="quickview_<?php echo $i;?>"> < ...

The verification of form is not done using an if statement

There are two forms in my code named formA and comments that I need to submit via AJAX. However, the current if and else conditions do not correctly identify the form and always trigger the alert message hello3. Here is the JavaScript function: function ...

I am struggling to get the pop-up to close using the current code. I suspect that the issue might be related to the variable I was previously using in WordPress. I have made changes but the pop-up

While delving deeper into the realm of Javascript, I encountered a stumbling block with a single popup intended for the main page of a WordPress website I am constructing. Despite my attempts to modify the code's variables, they seem unyielding. Surpr ...

Error encountered while scrolling with a fixed position

I am currently in the process of developing a carousel slider that resembles what we see on Android devices. The main challenge I am facing at this early stage is applying the CSS property position: fixed; only horizontally, as vertical scrolling will be n ...

I am looking to send data to an API whenever the user closes the browser tab

Hey guys, I need some help with a problem I'm facing in my current scenario. I am working on a feature to lock bookings for other admins, and I have created a field in the bookings table to store the admin's ID when they access the booking. Howev ...

Issue with Angular forms: The value of the first input element does not match the value set

I am still learning Angular, so please forgive me if my question seems a bit basic. Currently, I have a reactive form that retrieves data for editing from my controller. It seems to be working but there are some bugs present. Controller: myForm:any ...

Is there a way to adjust the state value in Pinia within a Vue3 component test, and have an impact on the component?

When testing the component using pinia with vue-test-utils, I encountered difficulty in modifying the state value stored in pinia. Despite trying multiple methods, I was unable to achieve the desired result. The original component and store files are provi ...

Transforming a sequence of characters into a multidimensional array

I have a nested for loop that generates an empty string value representing a multidimensional array. After the loops finish, the output looks like this: "[[0,0,0,0],[0,0,0,0]]" Now, I want to insert this into a multidimensional array within my code. How ...

Changing the color of a highlighted face on a PlaneGeometry in ThreeJS

I am trying to figure out how to change the color of a specific face when hovering over it in my PlaneGeometry, but I am having trouble getting the selected face. Below is the code I have so far: //THREE.WebGLRenderer 69 // Creating a plane var geometr ...

Sending a javascript variable to an angularjs scope

Currently, I am utilizing Flask to render an HTML template and wish to transfer the variable add_html_data, which is passed via Flask's render_template, to the scope of an AngularJS controller. I have attempted the following: <body> <di ...

When attempting to assign a 'string' type to the @Input property, I am receiving an error stating that it is not assignable to the 'PostCard Layout' type

Encountering an issue The error message 'Type 'string' is not assignable to type 'PostCard Layout'' is being displayed A parent component named page-blog.component.html is responsible for defining the class styles and passi ...

Creating and fetching CSV files using PHP and AJAX

I have successfully created a PHP script that generates a CSV file for automatic download by the browser. The code below shows a working version with sample data for different car models. <?php $cars = array( array("Volvo",22,18), array("BMW",15,1 ...

Changing a datetime string into a specific unit of time (such as seconds) using Javascript

Struggling with converting a Rails timestamp into a usable format for JavaScript. When alerting the time of an event, the string produced is: 2016-02-18T23:07:00.000-08:00. It doesn't appear to be a UTC string, according to this resource, and I' ...

Developing a nested JSON structure

I'm struggling with a seemingly simple task of creating a JSON object. Despite my efforts, I can't seem to find the right information to guide me through it. Here is what I have so far: var myJsonObject = new Object(); myJsonObject.context.appli ...

What type does a React stateless functional component Flow return?

If I have a structure like this const RandomComponent = (props) => ( <div> <SomeSubComponent id={props.id} /> <AnotherSubComponent type={props.type} /> </div> ) How do I specify the return type using Flow, i.e., wha ...

`The enforcement of a function's parameter requiring it to be part of the generic type T`

Is there a way in my typescript function to ensure that all keys in the second argument belong to the object defined by the first argument? For example: mapTo([new KeyValue('a', 'b'), new KeyValue('x', 'y')], {key ...

Display the name of the link on the console

I am working on an HTML list that contains some links: <ul> <li><a href="#" name="link1">link1</a></li> <li><a href="#" name="link2">link2</a></li> <li><a href="#" name="link3">link3& ...