javascript - Break D3 Each Loop Without a flag - Stack Overflow

Consider the following code:circle.each(function (d) {...code});How can I break the loop? Is there a

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
});

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
});
Share Improve this question edited Sep 14, 2015 at 14:21 taxicala asked Sep 14, 2015 at 14:10 taxicalataxicala 21.8k7 gold badges40 silver badges66 bronze badges 1
  • i've edited my question in order to avoid confusions. Code is not relevant. everything is working I just want to know how to properly break the loop if its posible. – taxicala Commented Sep 14, 2015 at 14:22
Add a ment  | 

2 Answers 2

Reset to default 6

No, there is not. Take a look at the each source code https://github./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.

you can do following

var flag = false;
circle.some(function (d) {
    if (flag) return true;
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205605a4616578.html

相关推荐

  • javascript - Break D3 Each Loop Without a flag - Stack Overflow

    Consider the following code:circle.each(function (d) {...code});How can I break the loop? Is there a

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信