Search code examples
javascriptarraysd3.jsdynamic-arrays

D3: How to detect the end of the .each()


In D3.js, I am traversing through data using the following method:

elem.allDataRows.each(function(d, i) {
    elem.checkboxMemory[i] = {
        id: d.key,
        isChecked: d.isChecked
    };
});

Since this process is asynchronous, I need to be able to determine when the loop process is complete.

Is their any way in D3 that can I detect the end of the .each()? Not for every interval, but the entire process.

Just for the record, elem.allDataRows is an array.

Thank you!


Solution

  • One simple way is to check value of the index i:

    function(d, i) {
        if (i == elem.allDataRows.length - 1){
            // last element
        }
        elem.checkboxMemory[i] = {
            id: d.key,
            isChecked: d.isChecked
        };
    }