none
Help with using a forEach with an Array

    Question

  • I'm new to web development and I am trying to get a forEach to cycle through an array of lists with many items. There are two forEach loops, the inner one that is counting the item returned is working fine on it's own, but the forEach to iterate through the lists returns me no results. The code only includes two test lists, but will be used with up to 35 lists with as many as 1000 items in many of the lists. Any insights would be greatly appreciated.

    private _getListCompletedTotals(): void {
    let html: string = "";
    var itemsCompleted= 0;
    var itemsLength = 0;
    const element: Element = this.domElement.querySelector("pnpCompletedItems");
    let workLists  = ["Data List", "Data List 2"];
    html += 'Dashboard Lists' + '<br/>';
    workLists.forEach ((item) => {
    pnp.sp.web.lists.getByTitle(item).items.select("Completed").filter("Completed eq 'Yes'").get().then((items: any[]) =>{
    itemsLength = items.length;
            items.forEach(element => {
    itemsCompleted += 1;
    });
    });
    html += 'List Title: ' + item + ' Items Completed: '+ itemsCompleted + '<br/>';
    element.innerHTML = html;
    });
    }

    • Edited by ArtsDev Monday, December 11, 2017 3:44 PM
    Monday, December 11, 2017 3:38 PM

All replies

  • Hi ArtsDevG,

    According to your description and codes, we couldn't directly find the reason why you couldn't get the right itemcompleted number.

    I suggest you could consider using F12 browser development tool to set a breakpoint at your codes to see how you codes works and why it doesn't get the right result.

    More details about how to use browser development tool to set the breakpoint,I suggest you could refer to this article.

    Best Regards,

    Brando


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 28, 2017 7:29 AM