0

I have this do while loop

trucks = [];
fetchdata(){
    this.trucks = [];

    do {

     this._reportService.getReports({
        ..//pagination stuff here
        })
        .subscribe(res=> {
          this.trucks.push(res.rawdatatrucks);
        })
    } while(i<this.totalRecords);
    console.log(this.trucks)

}

THe above works but data is pushed in this form

0:[
    0:[ ...truck data stuff ],
    1:[ ...truck data stuff ],
    2:[ ...truck data stuff ],
    3:[ ...truck data stuff ],
 ],
 1:[
    0:[ ...truck data stuff ],
    1:[ ...truck data stuff ],
    2:[ ...truck data stuff ],
    3:[ ...truck data stuff ],
 ],

What i was looking forward to get is to append the data at the end of the array such that i would get

console.log(this.trucks)

     0:[ ...truck data stuff ],
    1:[ ...truck data stuff ],
    2:[ ...truck data stuff ],
    3:[ ...truck data stuff ],
    4:[ ...truck data stuff ],
    5:[ ...truck data stuff ],
    6:[ ...truck data stuff ],
    .......

A response from the server that is

console.log(res.rawdatatrucks);

always starts at 0 even in the other iterations of the while loop.

What else do i need to add to the .push method

1 Answer 1

3

Use concat instead of push.

this.trucks = this.trucks.concat(res.rawdatatrucks);

Sign up to request clarification or add additional context in comments.

2 Comments

concat fails to work. It leaves it as an empty array
Remember that concat() on its own doesn't modify anything, it returns a new array which you'll have to assign back onto this.trucks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.