Using AngularJS to make repeated API calls with modified parameters

Issue - My task involves consolidating the response array into one. I am making consecutive calls to the same API, with a dynamic 'skip' parameter based on the last response.

Call #1 - api(id, skip=0)
Call #2 - api(id, skip+1)
...

Below is the implementation:

function combineArrays(jobId, skip, count) {
  appSegmentService.csvDownload(jobId, skip, count)
    .then(function(resp) {
      var finalArray = [];

      finalArray = finalArray.concat(resp.data);
      skip += 5000;

      if (resp.data.length === 5000) {
        finalArray = finalArray.concat(combineArrays(jobId, skip, count));
      } else {
        console.log(finalArray);
      }

    }, function(err) {
      console.error(err);
    });
}

Answer №1

For all your array operations, I highly recommend using the lodash package. It is a versatile tool that can make your tasks much easier. You can find more information about it at https://lodash.com/docs/

var _ = require('lodash');
var arr=[];
function addItemsToArray(itemId, start, total) {
                    var localList=appService.retrieveData(itemId, start, total)
                        .then(function (response) {
                            var items = [];

                            items = items.concat(response.data);
                            start += 5000;

                            if (response.data.length === 5000) {

                               return items = items.concat(addItemsToArray(itemId, start, total));
                            } else {
                                console.log(items);
                            }

                        }, function (error) {
                            console.log(error);
                        });
                       arr=_.concat(arr,localList);
                       return arr;

                }

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

Integrate, Delay, Experimentalize, and Attach components

This inquiry might lean more towards a general browser/javascript discussion rather than a focused prototype question, but I believe this community possesses a deep understanding of javascript and browsers. With that said, here is my query: If the followi ...

Using Angular Material for creating tabs with identical content

I am using material with angularjs and have encountered an issue with my two md-tabs. Both tabs have a similar DOM structure, but contain some unique content. Instead of duplicating the common DOM twice, I am looking for an alternative solution. Is it poss ...

Tips for accessing information from an Angular Resource promise

I am currently facing an issue when trying to read data from an angular promise that was returned. Before, I had the following code: var data = category.get({id:userid}); Later, I realized that the data being returned was an unresolved promise. So, I ma ...

Accessing Values from a Map Returned by Spring in an Angular Controller

I'm completely new to using Angular and I need some help with retrieving a map value returned from Spring inside an Angular controller. Could someone please assist me with this? Below is the code snippet for reference: app.js // Service --------- ...

Finding the inverse value from the Lodash get() function

My current approach involves utilizing the lodash get() method to retrieve values from an object. However, there are instances where I need to obtain negated values. Unfortunately, simply applying a negate symbol or negate method after retrieving the valu ...

Values in AngularJS are essential components that allow data

What is the best way to access the value of FirstName in my directive? <hello-World value="{{item.FirstName}}"> </hello-World> app.directive('helloWorld', function() { return { restrict: 'AE', replace: 'true&apos ...

What is the best way to retrieve a value from a database and display it in a radio

Can you help me figure out how to implement this functionality? I have a database with a "boolean" field that stores 0 or 1 values. On an HTML page, there is a radioButton with options for OK or NO. I need to dynamically load the boolean value from the dat ...

What is the best way to transfer a property-handling function to a container?

One of the main classes in my codebase is the ParentComponent export class ParentComponent extends React.Component<IParentComponentProps, any> { constructor(props: IParentComponent Props) { super(props); this.state = { shouldResetFoc ...

The d3 hierarchy possesses the capability to compute the average values of child nodes

Looking for a solution with d3 visualization that involves averaging up the value of score on the lowest nodes and dynamically adding that average to the parent node above. It seems like there isn't an easy method in d3 for this task. The desired outc ...

Scrolling seamlessly within a container that is fixed in position

For hours, I've been attempting to incorporate smooth scrolling into my project with no success. The issue lies in the container where the anchor tags are located. If it's set to fixed position or absolute, none of my attempts seem to work. In ...

Retrieving Browser URL using Node.js

Currently, I am trying to extract the browser URL from a user who has integrated my external JavaScript file into their website. The process involves them including the JS file, which triggers an Ajax call using jQuery to communicate with my Node server. ...

The background failed to display (potentially due to a hovering function)

I encountered an issue with a div that has a background image. Upon loading the page, the background image fails to display initially. However, when I hover over the div and then move my mouse elsewhere (due to a specific function described below), the bac ...

Split the input string into individual characters for each entry

As a beginner in Angular, I am currently exploring ways to split a single input field into separate inputs for each word. I attempted to achieve this using jQuery but struggled and also learned that mixing jQuery with Angular is discouraged. Here's th ...

A step-by-step guide on accessing grouped column data in Angular UI Grid

How do we access the data of all rows within a grouped column in Angular UI Grid? For instance, when looking at the Grouping Tutorial, how can we retrieve all the company names from the 'Company' column? I have successfully obtained the aggrega ...

Utilizing inline conditionals in Angular.js to automatically add plural words

I encountered an issue while attempting to create a condition in Angular. I aim to display "product selected" when the length is 0 or 1. For any other length, I want to display "products selected". I tried the code below but was unable to achieve the de ...

Error encountered when uploading mp3 file to S3 node due to size limitation

Currently, I am utilizing Node to transmit mp3 files to Amazon S3. However, after uploading the file, it shows a size of 9.0 Bytes and when attempting to play the audio from the public URL, it does not work as expected. Here is my current code snippet: rou ...

What is the best way to bring in styles to a Next.js page?

I am facing an issue with my app where I have a folder called styles containing a file called Home.module.css. Every time I try to include the code in my pages/index.js, I encounter the same error message saying "404 page not found.." import styles from & ...

What is the process for retrieving User Information using AngularJS following NodeJS authentication?

While I've come across similar questions on this topic, my lack of experience with Angular and Node is making it difficult for me to find a suitable solution. I had previously written this code that successfully handled the login process and allowed ...

React/NextJS: Firebase Realtime Database displaying the message "Error: Client is offline"

Encountering an occasional error when using React with NextJS to fetch data from a Firebase Realtime Database. Unhandled Runtime Error Error: Error: Client is offline. Currently utilizing Firebase version 9.0.1 for React. The initialisation and configura ...

Angular UI Router allows for the resolution of data before a state

When working on my angular sample route, I attempted to achieve the following: (function(angular){ 'use strict'; angular .module('appRouter',['ui.router']) .controller('routerCtrl',function( ...