Angular modules are designed to repeat chunks of code in a

Whenever I scroll the page, my function pushes items to an array. However, I am facing an issue where the pushed items are repeating instead of adding new ones.

Solution Attempt

onScroll(): void {
      console.log('scrolled');
      var i,j,newArray,chunk = 10;
      for (i=0,j=this.listOfData.length; i<j; i+=chunk) {
          newArray = this.listOfData.slice(i,i+chunk);
          this.results = this.results.concat(newArray);
      }
}

The this.listOfData array contains 19 items but only shows 19 to 10 in a loop, repeating, while items from 9 to 1 never appear.

Original code source

Any suggestions on how to fix this issue?

Update

If it's unclear, I have created a short video demonstrating the problem Watch here

Answer №1

Your code is not working properly because of the issue pointed out by Aditya in the preceding answer. You should make changes to your code as follows:

       handleScroll(): void {
          console.log('User has scrolled.');
          var index, newArray, chunkSize = 10;
          if (this.results.length == 0) {
              newArray = this.listOfData.slice(0, chunkSize);
              this.results = this.results.concat(newArray);
          } else if (this.results.length < this.listOfData.length) {
             index = this.results.length - 1;
             newArray = this.listOfData.slice(index, index + chunkSize);
             this.results = this.results.concat(newArray);
          }
       }

Answer №2

Each time your function runs, the i value is reset to 0 and the chunk is always set to 10, resulting in the same chunk being sliced from the array repeatedly. This explains why you are experiencing duplicate data.

If you are confused about whether Array.prototype.slice modifies the original array, it actually does not make any changes to the original array.

To address this issue, consider modifying your code as follows:

onScroll(): void {
      console.log('scrolled');
      var i,j,newArray,chunk = 10;
      for (i=0,j=this.listOfData.length; i<j; i+=chunk) {
          newArray = this.listOfData.splice(i,i+chunk);
          this.results = this.results.concat(newArray);
      }
}

By using Array.prototype.splice, the specified chunk will be removed from the original array and returned as a separate chunk.

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

Demonstration of Concurrent Page Processing in Flask

Currently, I am working on an application that heavily utilizes graphics using libraries such as Raphael and graphdracula. The main functionality of the application involves drawing various graphs across different pages named graph1, graph2, and graph3. L ...

The declaration file for module 'react/jsx-runtime' could not be located

While using material-ui in a react project with a typescript template, everything is functioning well. However, I have encountered an issue where multiple lines of code are showing red lines as the code renders. The error message being displayed is: Coul ...

The issue with AngularJS multiple $http requests failing to fetch accurate data

I'm having issues with my AngularJS controller and service modules. I want to refresh custController.allCustomers after adding a new customer so that the UI displays the new data. However, when I call custController.createCustomer, the new customer do ...

Ensuring a button (class) is in active hover mode when the document loads

My website features 4 clickable service buttons on the homepage, each with a unique hover effect (background-color change) when users interact with them. While this setup works well overall, I am looking to highlight the FIRST button as the most important ...

The timer freezes briefly at 1:60 before switching to 1:01

I'm in the process of creating a website for Rubik's cube scrambling and timing, and everything seems to be working well so far. I have implemented a scrambler, a timer, and a list of recorded times (still a work in progress). The timer is functi ...

Tips for concealing broken images in jQuery/JavaScript when generating dynamic content

Is there a way to hide the broken images in this dynamically generated HTML code using JavaScript? Unfortunately, I don't have access to the source code itself. I'm new to JQuery and could really use some assistance. Below is the snippet of the ...

The function document.getElementById(...) is failing to retrieve the text data from the table as expected

Greetings fellow HTML and JavaScript novice! I've got a table with input cells in the bottom row: <table class="convtbl" id="table1"> <tr> <td>Distance 1 (in miles)</td> <td>Time ...

Do I have to cram all the content onto a single page just to use a scroll effect?

I'm currently facing a dilemma as I work on building my portfolio. My goal is to primarily use html5 and css3, along with a bit of js, jquery, and other tools if necessary. Although I am not an expert in web development, I wanted to push myself to cre ...

Dynamic content modal

Recently, I started exploring react native. In my application, there are 5 buttons on the screen. Each of them triggers the same <Modal>, but the content inside it changes based on the button clicked. For example, if I click the first button, a tex ...

dynamic image size based on mobile device orientation

Struggling with a tiny mobile image gallery here. Whenever I switch to portrait mode, the images end up being too big for the screen. Is there a way to use javascript to resize the images to fit the screen when switching to portrait mode? They display fin ...

``The presence of symlink leading to the existence of two different versions of React

Currently, I am working on a project that involves various sub custom npm modules loaded in. We usually work within these submodules, then publish them to a private npm repository and finally pull them into the main platform of the project for use. In orde ...

Preventing Redundancy in Angular 2: Tips for Avoiding Duplicate Methods

Is there a way I can streamline my if/else statement to avoid code repetition in my header component? Take a look at the example below: export class HeaderMainComponent { logoAlt = 'We Craft beautiful websites'; // Logo alt and title texts @Vie ...

Sharing parameters between functions in JavaScript

I have a working code but I want to modify the function getLocation to accept 2 arguments that will be passed to getDistanceFromLatLonInKm within the ajmo function. Currently, getDistanceFromLatLonInKm has hardcoded arguments and I would like to use variab ...

Struggling to deploy a basic node.js application to Heroku or connect it with MongoDB

Currently, I'm facing some challenges trying to deploy a simple Twitter-like app to HEROKU or MongoDB. However, I seem to be hitting roadblocks with both platforms. With MongoDB, I encounter either an internal server error or the actual code displayin ...

Is it possible to retrieve HTML content using YQL?

Imagine the scenario where you need to retrieve information from a web page structured like this: <table> <tr> <td><a href="Link 1">Column 1 Text</a></td> <td>Column 2 Text</td> <td>Colum ...

Preserve component state in Angular 4 by saving it when navigating to a different component

Currently, I am faced with a challenge while developing an application using Angular. In one component, users can input data into three selectboxes and then initiate a search to find matching results. Upon clicking on a match, they are taken to another com ...

Error: Undefined object property 'name' TypeError: Undefined object property 'name'

Currently, I am working on a project that involves AngularJS and .net core. The main concept is to use Angular for presenting data and calling WebAPI from .net core. Presenting the data is not an issue, but I have encountered a problem with posting data as ...

Scroll Bar Menu (User-Controlled)

I'm looking for any libraries out there that can replicate or provide a similar horizontal scrolling menu to that of espn.com's homepage. I'm relatively new to jquery and feeling a bit lost on where to begin. I did some searching on Google, ...

Tips for preventing a span from disappearing when a hidden div is displayed during onmouseover

, there is a concern about the disappearing behavior of the span with ID "topnavbar" when the display property changes from none to block on a hidden div using onmouseover event. The query is how to ensure that the span remains visible at all times. Additi ...

Ways to automatically include a local variable in all functions

In each of my functions, I start with this specific line: var local = {} By doing so, it allows me to organize my variables using the following structure: local.x = 1 local.y = 2 Is there a way to modify all function prototypes to automatically include ...