Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break D3 Each Loop Without a flag

Consider the following code:

circle.each(function (d) {
    //...code
});

How can I break the loop? Is there a natural D3 way to break out of an each loop? I mean without a flag as follows:

var flag = false;
circle.each(function (d) {
    if (flag) return;
    if (someCondition) flag = true;
    //...code
});

I've tried returning false inside the if statement but it did not work (thought that maybe this would work the same as jquery.each but I was wrong):

circle.each(function (d) {
    if (someCondition) return false; //Not working
    //...code
});
like image 634
taxicala Avatar asked Feb 09 '23 01:02

taxicala


2 Answers

No, there is not. Take a look at the each source code https://github.com/mbostock/d3/blob/78e0a4bb81a6565bf61e3ef1b898ef8377478766/src/selection/each.js.

You may be able to throw an exception to break the loop, but unless your case is really "exceptional", using an exception is probably more confusing than helpful.

like image 110
Jeff Storey Avatar answered Feb 11 '23 15:02

Jeff Storey


you can do following

var flag = false;
circle.some(function (d) {
    if (flag) return true;
});
like image 22
shyam_ Avatar answered Feb 11 '23 14:02

shyam_